Many Relationship for Many ASP.NET Entity Framework

Asked

Viewed 138 times

2

I am having difficulties to make the relationship between my tables using EF.

When creating an Order, I need to list the data recorded in other tables, such as Equipment, Tasks and Person, as I try to represent in the class below:

public class OrdemDeManutencao
{
    [Key]
    [Required]
    [Display(Name = "Código")]
    public int ID { get; set; }

    //como fazer relacionamento muitos para muitos??
    public List<Equipamentos> Equipamentos { get; set; }
    public List<Tarefas> Tarefas { get; set; }
    public List<Pessoa> Tecnicos { get; set; }

    [Display(Name = "Data de Abertura")]
    [DataType(DataType.Date)]
    public DateTime DataAbertura { get; set; }

    [Display(Name = "Data Realizado")]
    [DataType(DataType.Date)]
    public DateTime DataRealizado { get; set; }

    public StatusOrdem Status { get; set; }

    public PrioridadeOrdem Prioridade { get; set; }
}

How I can list the records made in Equipment, Tasks and Person, in the Order?

Thank you in advance!

1 answer

2

If it is of N-N you will have to create first of all intermediate tables for each entity in order not to interfere in the table structure.

If it is 1-N between

Ordemmanutencao-Equipamentos Ordemmanutencao-Tasks Ordemmanutencao-Tecnicos

Using Fluent Api

HasRequired(hr => hr.OrdemDeManutencao)
    .WithMany(wm => wm.Equipamentos)
    .HasForeignKey(fk => fk.OrdemDeManutencaoFK);

HasRequired(hr => hr.OrdemDeManutencao)
    .WithMany(wm => wm.Tarefas)
    .HasForeignKey(fk => fk.OrdemDeManutencaoFK);

HasRequired(hr => hr.OrdemDeManutencao)
    .WithMany(wm => wm.Pessoas)
    .HasForeignKey(fk => fk.OrdemDeManutencaoFK);

public class Equipamentos
{

    public virtual OrdemDeManutencaoFK { get; set; }

    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }

}

public class Tarefas
{

    public virtual OrdemDeManutencaoFK { get; set; }

    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }

}

public class Tecnicos
{

    public virtual int OrdemDeManutencaoFK { get; set; }

    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }

}

Using Dataannotations

public class Equipamentos
{
    public virtual OrdemDeManutencaoFK { get; set; }

    [ForeignKey("OrdemDeManutencaoFK")]
    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }
}

public class Tarefas
{

    public virtual OrdemDeManutencaoFK { get; set; }

    [ForeignKey("OrdemDeManutencaoFK")]
    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }
}

public class Tecnicos
{
    public virtual int OrdemDeManutencaoFK { get; set; }

    [ForeignKey("OrdemDeManutencaoFK")]
    public virtual OrdemDeManutencao OrdemDeManutencao { get; set; }
}

PS: Don’t forget to leave as virtual and when it comes to lists, leave as the type Icollection. The virtual issue is the nomenclature pro Entity Framework can validate when it is Lazy loading (if you want to use) and Icollection is because I’ve had problems with it and it didn’t pull Lazy loading.

public class OrdemDeManutencao
{
    [Key]
    [Required]
    [Display(Name = "Código")]
    public int ID { get; set; }

    //como fazer relacionamento muitos para muitos??
    public virtual ICollection<Equipamentos> Equipamentos { get; set; }
    public virtual ICollection<Tarefas> Tarefas { get; set; }
    public virtual ICollection<Pessoa> Tecnicos { get; set; }

    // resto da classe
}

Browser other questions tagged

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