How to bring a city relationship into operation?

Asked

Viewed 84 times

-5

I have this model

public class Funcionario
  {
        [Key]
        public int id { get; set; }
        [Required(ErrorMessage ="Nome do funcionário é obrigatório", AllowEmptyStrings =false)]
        [Display(Name ="Nome")]
        public String nome { get; set; }
        [Required(ErrorMessage = "Data de Nascimento do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "Data de Nascimento")]
        public DateTime dataNascimento { get; set; }
        [Required(ErrorMessage = "CPF do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "CPF")]
        public long cpf { get; set; }
        [Required(ErrorMessage = "Cidade do funcionário é obrigatório", AllowEmptyStrings = false)]
        [Display(Name = "Cidade")]
        public virtual int cidade { get; set; }
    }

and that other

public class Cidade
    {
        [Key]
        public int id { get; set; }
        [Required(ErrorMessage = "O nome da cidade é obrigatório", AllowEmptyStrings = false)]
        [Display(Name="Nome")]
        public String nome { get; set; }
    }

Note that the Employee class receives City and I need to show the name of the city on the Grid and not just the code. Then I ask: Should I bring a City Survey in operation? And how does it look in the City model? a virtual int ????

  • When I posted, I’ve been downvoted without reading, how fast they are

  • 6

    Dude, you have a lot of doubt about C# and ASP.NET MVC, and some basic questions, as they are often, I advise you to read books and see courses on technology, I’m sure you won’t have to waste time with this level of question.

  • 1

    I never know if these questions are serious or a kind of test when the user is a user with so many points of reputation

  • Even more so when the nickname and the main tag of the user indicate that it already uses a lot . net

1 answer

1

It depends on how you desire your relationship, if you are following the line that an Employee is in a City and a City has multiple employees, the relationship would be as follows:

public class Funcionario
{
    //Essa propriedade representa a chave estrangeira da cidade
    public int IDCidade { get; set; }

    //Essa propriedade representa a entidade da cidade
    [ForeignKey("IDCidade")]
    public virtual Cidade Cidade { get; set; }
}

public class Cidade
{
    public Cidade()
    {
        Funcionarios = new List<Funcionario>();
    }

    //Essa propriedade representa a coleção de funcionários
    public ICollection<Funcionario> Funcionarios { get; set; }
}
  • I’ll leave a link for you with lots of information about the settings of the Entity Framework. I find a site very interesting and useful. Link: http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx

  • I corrected the employee model because I had forgotten to change the type of the City. I was int Cidade, but the right thing is Cidade Cidade

Browser other questions tagged

You are not signed in. Login or sign up in order to post.