实体框架一个方向一对多级联删除

时间:2013-04-09 13:40:28

标签: entity-framework-4 ef-code-first cascading-deletes

我有两个实体:

public class Report : SimpleEntity
{
     public virtual ICollection<BaseFrequency> Frequencies
    {
        get
        {
            return _requencies ?? (_requencies = new HashSet<BaseFrequency>());
        }

        set
        {
            _requencies = value;
        }
    }
}

public class CustomReport : SimpleEntity
{
   public virtual string Name {get; set;}

   public Report Report { get; set; } 
}

映射:

internal class BaseFrequency: EntityMappingBase<BaseFrequency>
    {
        public BaseFrequency()
        {
            Property(x => x.Name).HasMaxLength(250).IsRequired(); 
        }
    }

internal class MyReportMapping : EntityMappingBase<MyReport>
{
    public MyReportMapping()
    {
        HasMany(x => x.Frequencies).WithOptional(x => x.Report).HasForeignKey(x => x.ReportId).WillCascadeOnDelete(true);         
    }
}

当我删除报告时,我得到了“

  

DELETE语句与REFERENCE约束冲突   \ “Report_Frequencies \”。冲突发生在数据库中   \“SamAssist \”,表\“dbo.BaseFrequencies \”,列   'ReportId'。\ r \ n语句已终止。“}错误DELETE   语句与REFERENCE约束冲突   \ “Report_Frequencies \”。冲突发生在数据库中   \“SamAssist \”,表\“dbo.BaseFrequencies \”,列   'ReportId'。\ r \ n该声明已被终止

数据库限制为enter image description here

我该怎么做才能修复这个错误?

0 个答案:

没有答案