5
I’m trying to map the same entity twice into another
public class Conveniado
{
public int Id { get; set; }
public string Nome { get; set; }
[InverseProperty(nameof(ProcedimentoAgregado.ConveniadoDe))]
public virtual ICollection<ProcedimentoAgregado> ProcedimentoAgregadoConveniadosDe { get; set; }
[InverseProperty(nameof(ProcedimentoAgregado.ConveniadoPara))]
public virtual ICollection<ProcedimentoAgregado> ProcedimentoAgregadoConveniadosPara { get; set; }
}
public class Procedimento
{
public int Id { get; set; }
public string Descricao { get; set; }
[InverseProperty(nameof(ProcedimentoAgregado.ProcedimentoDe))]
public virtual ICollection<ProcedimentoAgregado> ProcedimentoAgregadoProcedimentosDe { get; set; }
[InverseProperty(nameof(ProcedimentoAgregado.ProcedimentoPara))]
public virtual ICollection<ProcedimentoAgregado> ProcedimentoAgregadoProcedimentosPara { get; set; }
}
public class ProcedimentoAgregado
{
[Key]
public Guid ProcedimentoAgregadoId { get; set; }
[Display(Name = "Conveniado de")]
public int ConveniadoDeId { get; set; }
[Display(Name = "Procedimento de")]
public int ProcedimentoDeId { get; set; }
[Display(Name = "Conveniado para")]
public int ConveniadoParaId { get; set; }
[Display(Name = "Procedimento para")]
public int ProcedimentoParaId { get; set; }
[ForeignKey(nameof(ConveniadoDeId))]
public virtual Conveniado ConveniadoDe { get; set; }
[ForeignKey(nameof(ConveniadoParaId))]
public virtual Conveniado ConveniadoPara { get; set; }
[ForeignKey(nameof(ProcedimentoDeId))]
public virtual Procedimento ProcedimentoDe { get; set; }
[ForeignKey(nameof(ProcedimentoParaId))]
public virtual Procedimento ProcedimentoPara { get; set; }
}
When rotating the udpate-database
I have the following error
Introducing FOREIGN KEY Constraint 'Fk_dbo. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or Modify other FOREIGN KEY constraints. Could not create Constraint or index. See Previous errors.
I always solved it using the OnModelCreating
of the context
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProcedimentoAgregado>()
.HasRequired(a => a.ProcedimentoPara)
.WithMany(a => a.ProcedimentoAgregadoProcedimentosPara)
.HasForeignKey(a => a.ProcedimentoParaId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<ProcedimentoAgregado>()
.HasRequired(a => a.ConveniadoPara)
.WithMany(a => a.ProcedimentoAgregadoConveniadosPara)
.HasForeignKey(a => a.ConveniadoParaId)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
Or
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
base.OnModelCreating(modelBuilder);
}
I wonder if there’s any other way to solve this than by using these two ways?
Why you need to map twice if you are an entity and you already have a Collection of them?
– Leandro Angelo
'Cause it’s system business rule.
– Pablo Tondolo de Vargas
But should it be implemented in this layer? Precisely because it is a business rule?
– Leandro Angelo
And in what layer would it have to be if not in the entity?
– Pablo Tondolo de Vargas
If it is a business rule, it should not be in the entity, because its role is to reflect the structure of the bank, if you have an auxiliary table with two fks for the same table (Origin and Destination) you should have a specific Entity for it. But I’m just speculating, add the diagram of your data structure
– Leandro Angelo
But at what point would my entity not be reflecting the structure of the bank? I use Migrations by EF, my database structure is what is in the mapping of my entities.
– Pablo Tondolo de Vargas