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
}