简体   繁体   English

实体框架迁移级联删除警告

[英]Entity Framework Migration Cascading Delete Warning

I have two classes that relate to one another (one-to-many) and I thought I had the properties setup correctly, but when I run the Update-Database command for my migration, I get the following error: 我有两个相互关联的类(一对多),我以为我已正确设置了属性,但是当我为迁移运行Update-Database命令时,出现以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID' on table 'ParentEnrollment' may cause cycles or multiple cascade paths. 在表“ ParentEnrollment”上引入FOREIGN KEY约束“ FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint or index. 无法创建约束或索引。 See previous errors. 请参阅先前的错误。

My two classes: 我的两个班:

[Table("CellGroup")]
public class CellGroup : BaseEntity
{
    public Guid AcademicYearID { get; set; }

    [ForeignKey("AcademicYearID")]
    public virtual AcademicYear AcademicYear { get; set; }

    public Guid LeaderID { get; set; }

    [ForeignKey("LeaderID")]
    public virtual Parent Leader { get; set; }

    public Guid PreviousGroupID { get; set; }

    [ForeignKey("PreviousGroupID")]
    public virtual CellGroup PreviousGroup { get; set; }

    public string Name { get; set; }

    public int MaximumSize { get; set; }

    public virtual ICollection<ParentEnrollment> Parents { get; set; }
}

and

[Table("ParentEnrollment")]
public class ParentEnrollment : BaseEntity
{
    public Guid ParentID { get; set; }

    [ForeignKey("ParentID")]
    public virtual Parent Parent { get; set; }

    public Guid AcademicYearID { get; set; }

    [ForeignKey("AcademicYearID")]
    public virtual AcademicYear AcademicYear { get; set; }

    public bool FirstTimeEnrolling { get; set; }

    public string HSLDAAccountNumber { get; set; }

    public DateTime HSLDARenewalDate { get; set; }

    public string CurrentChurch { get; set; }

    public string CurrentChurchContact { get; set; }

    public string CurrentChurchPhone { get; set; }

    public Guid CellGroupID { get; set; }

    [Required]
    [ForeignKey("CellGroupID")]
    public virtual CellGroup CellGroup { get; set; }

    public bool VolunteerBuyOut { get; set; }

    public Guid VolunteerPositionID { get; set; }

    [ForeignKey("VolunteerPositionID")]
    public virtual VolunteerPosition VolunteerPosition { get; set; }

    public string VolunteerPositionNotes { get; set; }

    public virtual ICollection<StudentEnrollment> StudentEnrollments { get; set; }
}

I only have the Parents property on the CellGroup class so I can easily access the list of enrollments in that cell group. 我在CellGroup类上只有Parents属性,因此我可以轻松访问该单元组中的注册列表。 I tried to remove the property to see if it cleared up the warning/error, but it did not. 我试图删除该属性,以查看它是否清除了警告/错误,但是没有清除。 Can someone spot where I have gone wrong with my model(s)? 有人可以发现我的模型出问题了吗?

This error says that you cannot introduce a foreign key from table ParentEnrollment to table CellGroup that has cascading delete enabled, because this will create multiple cascade paths, which is not allowed on SQL Server. 该错误表明您无法将表ParentEnrollment的外键引入启用了级联删除的表CellGroup中,因为这将创建多个级联路径,这在SQL Server上是不允许的。

According to the code you posted both tables have relations to a table Parent as well as AcademicYear , which are on non nullable FK columns, so EF will enable cascading on delete by default. 根据您发布的代码,两个表都与表ParentAcademicYear有关系,它们都位于不可为空的FK列上,因此默认情况下,EF将在删除时启用级联。 With another FK from ParentEnrollment to CellGroup there would be multiple cascade paths, eg Parent to CellGroup to ParentEnrollment and Parent to ParentEnrollment , and this is causing your error. 使用从ParentEnrollmentCellGroup另一个FK,将有多个级联路径,例如ParentCellGroupParentEnrollment以及ParentParentEnrollment ,这将导致您的错误。 Removing the Parent property won't solve this because there still is the same cascading path problem starting from table AcademicYear . 删除Parent属性不会解决此问题,因为从AcademicYear表开始仍然存在相同的级联路径问题。

So you have to disable cascading delete for your foreign key, which has to be done using Fluent API in your DbContext like this: 因此,您必须为外键禁用级联删除,这必须使用DbContext中的Fluent API来完成,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<ParentEnrollment>()
                .HasRequired(m => m.CellGroup)
                .WithMany(m => m.Parents)
                .HasForeignKey(m => m.CellGroupID)
                .WillCascadeOnDelete(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM