使用EF Code First删除级联

时间:2015-05-06 12:22:45

标签: c# entity-framework

我正在使用C#和.NET Framework 4.5.1开发实体框架代码第一6.1.3库。

我有这三个表: enter image description here

这些EntityTypeConfiguration个文件: 班级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);
    }
}

班级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);
    }
}

班级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();
    }
}

我想这样做:如果我在CODES表上删除了一行,我想删除AGGREGATIONSAGGREGATION_CHILDS表上的代码。

如何使用Entity Framework Code First做到这一点?

我必须添加.WillCascadeOnDelete(false);,因为我收到有关周期和多个级联路线的错误。

1 个答案:

答案 0 :(得分:1)

这个错误几乎让我的笔记本电脑透过窗户几次......基本上它不起作用的原因是this ......但好消息是,我们可以通过删除来解决这个问题已隐含定义的关系。

这种关系可以完全删除:

AggregationsChild > HasRequired > Code

原因是AggregationOne to Many的关系AggregationChild

Aggregation > HasMany > AggregationChild

Aggregation也依赖于Code

Aggregation > HasRequired > Code

现在AggregationChildAggregation的孩子,而AggregationCode的孩子。从AggregationChildCode的额外依赖是创建多个级联路径,因此请完全删除它,这应该可以解决您的问题......

要找出Code所属的AggregationChild,您首先需要获取AggregationChild的父级,然后导航到Code实体通过Aggregation ....

var aggregationChild = this.AggregationChildren.Include("Aggregation.Code")
                                               .First(ac => ac.Id == 123);
var code = aggregationChild.Aggregation.Code;