Problem in relationship with EF6 (Self-Relationship and Associative Class)

1

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

Original Model - SQL Server

MymodelinPowerTools

ClassRegion

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

Territory Class

[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; }
}

Association Class Employee Territory

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; }
}
    
asked by anonymous 29.09.2015 / 03:31

1 answer

2

If your intention is to use an associative table, you should create it and make the appropriate associations.

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

This 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; }
    }

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

Once you've done this, you'll already have that association you want. However, your Employee entity 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; }

This becomes 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 do not think that's what you want.

Change to this:

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

In this case, you are saying that an employee has a list of Employee Territory and a relationship with it.

Doing so, your template will look like this:

    
29.09.2015 / 14:23