Many relationship to many ASP.NET Entity Framework

2

I'm having trouble making the relationship between my tables using EF.

At the time of creating a Maintenance Order, I need to list the data registered 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 can I list the records made in Equipment, Tasks and Person in the Maintenance Order?

Thank you in advance!

    
asked by anonymous 29.10.2016 / 03:15

1 answer

2

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

If it is 1-N between

Ordering Equipment-Equipment Order Maintenance-Tasks Order Maintenance-Technicians

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: Do not forget to leave it as virtual and when it comes to lists, leave it as type ICollection . The issue of the virtual nomenclature is the pro Entity Framework being able to validate when it's lazy loading (if you want to use) and the ICollection is because I've had problems with it and it did not pull the 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
}
    
29.10.2016 / 03:36