I'm developing an Entity Framework Code First 6.1.3 library with C# and .NET Framework 4.5.1.
I have these three tables:
And these there EntityTypeConfiguration
files:
Class CHILDSConfiguration
:
class AGGREGATION_CHILDSConfiguration : EntityTypeConfiguration<AGGREGATION_CHILDS>
{
public AGGREGATION_CHILDSConfiguration()
{
HasKey(ag_ch => ag_ch.CHILD_CODE);
Property(ag_ch => ag_ch.CHILD_CODE)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(ag_ch => ag_ch.CHILD_CODE)
.HasMaxLength(20)
.IsRequired()
.HasColumnName("CODE");
Property(ag_ch => ag_ch.PARENT_CODE)
.HasMaxLength(20)
.IsRequired();
HasRequired(ag_ch => ag_ch.Aggregation)
.WithMany(ag => ag.AggregationChilds)
.HasForeignKey(ag_ch => ag_ch.PARENT_CODE);
HasRequired(ag_ch => ag_ch.Code)
.WithOptional(c => c.AggregationChild)
.WillCascadeOnDelete(false);
}
}
Class AGGREGATIONSConfiguration
:
class AGGREGATIONSConfiguration : EntityTypeConfiguration<AGGREGATIONS>
{
public AGGREGATIONSConfiguration()
{
HasKey(ag => ag.PARENT_CODE);
Property(ag => ag.PARENT_CODE)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(ag => ag.PARENT_CODE)
.HasMaxLength(20)
.IsRequired()
.HasColumnName("CODE");
Property(ag => ag.CREATED)
.HasMaxLength(50)
.IsOptional();
HasRequired(ag => ag.Code)
.WithOptional(c => c.Aggregation)
.WillCascadeOnDelete(false);
}
}
Class CODESConfiguration
:
class CODESConfiguration : EntityTypeConfiguration<CODES>
{
public CODESConfiguration()
{
HasKey(c => c.CODE);
Property(c => c.CODE)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(c => c.CODE)
.HasMaxLength(20);
Property(c => c.CODE_LEVEL)
.IsRequired();
Property(c => c.COMMISIONING_FLAG)
.IsRequired();
Property(c => c.IS_TRANSMITTED)
.IsRequired();
Property(c => c.TIMESPAN)
.HasMaxLength(50)
.IsOptional();
Property(c => c.USERNAME)
.HasMaxLength(50)
.IsOptional();
Property(c => c.SOURCE)
.HasMaxLength(50)
.IsOptional();
Property(c => c.REASON)
.HasMaxLength(200)
.IsOptional();
Property(c => c.ALTERNATE_CODE)
.IsOptional();
Property(c => c.HELPER_CODE)
.IsOptional();
}
}
I want to do this: if I delete a row on CODES
table, I want to delete that code on AGGREGATIONS
and on AGGREGATION_CHILDS
table.
How can I do that using Entity Framework Code First?
I've had to add .WillCascadeOnDelete(false);
because I get an error about cycles and multiple cascade routes.