2

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: enter image description here

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.

VansFannel
  • 45,055
  • 107
  • 359
  • 626

1 Answers1

1

That error almost saw my laptop through the window a few times... basically the reason why it's not working is this... But the good news is, we can fix this by removing a relationship which is already implicitly defined.

This relationship is the one you can remove completely:

AggregationsChild > HasRequired > Code

The reason is that Aggregation has a One to Many relationship with AggregationChild

Aggregation > HasMany > AggregationChild

Aggregation also has a dependency on Code

Aggregation > HasRequired > Code

Now since AggregationChild is a child of Aggregation, and Aggregation is a child of Code. The extra dependency from AggregationChild to Code is creating multiple cascade paths, so remove it entirely, and that should fix your issues...

To find out which Code the AggregationChild is part of, you'll first need to get the parent of AggregationChild and then navigate to the Code entity via Aggregation....

var aggregationChild = this.AggregationChildren.Include("Aggregation.Code")
                                               .First(ac => ac.Id == 123);
var code = aggregationChild.Aggregation.Code;
Community
  • 1
  • 1
Aydin
  • 15,016
  • 4
  • 32
  • 42