I’m guessing the entity is called OrdemCompra
:
public class OrdemCompra
{
...
public int UsuarioCadastroId { get; set;}
...
public virtual Usuario UsuarioCadastro { get; set; }
}
And in Usuario
:
public class Usuario
{
...
public virtual ICollection<OrdemCompra> OrdemCompraCadastrados { get; set; }
}
Only, as you are mapping several cardinalities 1 to N involving Usuario
and OrdemCompra
, there is no way to completely escape from Fluent API. Would need to have:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OrdemCompra>()
.HasRequired(oc => oc.UsuarioCadastro)
.WithMany(u => u.OrdemCompraCadastrados)
.HasForeignKey(oc => oc.UsuarioCadastroId)
.WillCascadeOnDelete(false);
}
In the end, it stays like this:
public class OrdemCompra
{
...
public int UsuarioCadastroId { get; set; }
public int UsuarioLiberacaoId { get; set; }
...
public virtual Usuario UsuarioCadastro { get; set; }
public virtual Usuario UsuarioLiberacao { get; set; }
}
public class Usuario
{
...
public virtual ICollection<OrdemCompra> OrdemCompraCadastrados { get; set; }
public virtual ICollection<OrdemCompra> OrdemCompraLiberados { get; set; }
}
And the mapping:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OrdemCompra>()
.HasRequired(oc => oc.UsuarioCadastro)
.WithMany(u => u.OrdemCompraCadastrados)
.HasForeignKey(oc => oc.UsuarioCadastroId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<OrdemCompra>()
.HasRequired(oc => oc.UsuarioLiberacao)
.WithMany(u => u.OrdemCompraLiberados)
.HasForeignKey(oc => oc.UsuarioLiberacaoId)
.WillCascadeOnDelete(false);
}
Put some examples that produce a not too wide response.
– Leonel Sanches da Silva