Relationship Problem with EF6 (Relationship and Associative Class)

Asked

Viewed 493 times

1

I need to know if my auto relationship and my associative class are created correctly according to the original model made in the SQL Server Diagram.

Original Model - SQL Server

inserir a descrição da imagem aqui

My model in Power Tools

inserir a descrição da imagem aqui

Classe Região

[Table("Regiao")]
public class Regiao
{
    [Key]
    public long RegiaoID { get; set; }
    public string RegiaoDescricao { get; set; }
    public virtual ICollection<Territorio> Territorios { get; set; }
}

Class Territory

[Table("Territorio")]
public class Territorio
{
    [Key]
    public long TerritorioID { get; set; }
    public string TerritorioDescricao { get; set; }

    public long RegiaoID { get; set; }

    [ForeignKey("RegiaoID")]
    public virtual Regiao Regiao { get; set; }
    public virtual ICollection<TerritorioEmpregado> TerritorioEmpregados { get; set; }

}

Employee class

[Table("Empregado")]
public class Empregado
{
    [Key]
    public long EmpregadoID { get; set; }
    public string PrimeiroNome { get; set; }
    public string UltimoNome { get; set; }
    public string Titulo { get; set; }
    public string TituloDeCortesia { get; set; }
    public DateTime DataNascimento { get; set; }
    public DateTime DataContratacao { get; set; }
    public string Endereco { get; set; }
    public string Cidade { get; set; }
    public string Regiao { get; set; }
    public string CodigoPostal { get; set; }
    public string Pais { get; set; }
    public string TelefoneResidencial { get; set; }
    public string Extensao { get; set; }
    public string Notas { get; set; }

    public virtual ICollection<TerritorioEmpregado> TerritorioEmpregados { get; set; }

    public virtual Empregado Empregado1 { get; set; }
}

Class Associative Territorioemployed

public class TerritorioEmpregado
{
    [Key]
    public long TerritorioEmpregadoID { get; set; }
    public long TerritorioID { get; set; }

    [ForeignKey("TerritorioID")]
    public virtual Territorio Territorio { get; set; }

    public long EmpregadoID { get; set; }

    [ForeignKey("EmpregadoID")]
    public virtual Empregado Empregado2 { get; set; }
}

1 answer

2


If your intention is to use an associative table, you must create it and carry out the appropriate associations.

In this case, you would need to create the entity Territories. As your model SQL Show, she will be the link between Territories and Employees.

That entity would look like this:

 public class TerritorioEmpregado
    {
        [Key]
        public int TerriTorioEmpregadoId { get; set; }

        public int TerritorioId { get; set; }
        public int EmpregadoId { get; set; }

        public virtual Territorio Territorio { get; set; }
        public virtual Empregado Empregado { get; set; }
    }

Remember that this is just an example. You can change it in the way that best suits you. But it should be in your code.

Once done, you will already have the association you want. However, your entity Employee has this association:

 public long TerritorioID { get; set; }

    [ForeignKey("TerritorioID")]
    public virtual Territorio Territorio { get; set; }
    public virtual ICollection<Empregado> Empregados1 { get; set; }
    public virtual Empregado Empregados2 { get; set; }
    public virtual ICollection<Territorio> Territorios { get; set; }

That would be unnecessary. In this part you are saying that your employee has 1 territory, 1 employee, a list of Territories and a list of employees. I don’t think that’s what you want.

Change to that:

 public virtual ICollection<TerritorioEmpregado> TerritorioEmpregado { get; set; }
 public virtual Empregado Empregados2 { get; set; }

In this case, you’re saying that an employee has a list of Territoriumemployed and a relationship with himself.

Doing this, your model will look like this: inserir a descrição da imagem aqui

  • Randrade, my Territory class after the modifications got too much navigation property, I don’t know if it was because you did the relationship between Territory and Region. In logic, a region can have several territories, the region would be: South, Southeast, Midwest etc. Another doubt I remained was in relation to the nomenclature of foreign keys, I could not leave with the prefix FK, as your model, yours was much better. I would like you to evaluate my code to see if I did something wrong, my question was edited and the image generated in my Power Tools was updated.

  • @Kellysoares does not mean to be "boring", but could you return to the original question and open another specific question? His initial question was how to use an associative table, and now he has two other questions derived from this answer. This is normal to happen, but it is better for the community to have the two questions with different doubts, when editing the same.

  • @Kellysoares I will be happy to answer your questions smoothly. But it would be easier for anyone looking over, if it is better organized.

  • Sorry Randrade, it is because here does not allow include images in comments, so I edited the question to confirm the changes you suggested, I did not save the previous image to put again. I can change the question if that settles.

  • I changed the doubt, I hope I improved the mistake I made. Thank you again!

  • @Kellysoares you did not make any mistakes. This is normal and happens often. I just think it’s best to leave everything organized. But open another question with doubt, which I answer to you with the greatest pleasure.

  • Randrade, you helped me once to load scripts into ASP.NET MVC, can you figure out the error in this other post I opened? http://answall.com/questions/89144/erro-ao-criar-janela-modal-com-jquery-em-asp-net-mvc

Show 2 more comments

Browser other questions tagged

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