Entity Framework 迁移级联删除警告
Entity Framework Migration Cascading Delete Warning
我有两个 class 相互关联 (one-to-many),我认为我的属性设置正确,但是当我 运行 Update-Database
命令我的迁移,我收到以下错误:
Introducing FOREIGN KEY constraint
'FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID' on table
'ParentEnrollment' may cause cycles or multiple cascade paths. Specify
ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN
KEY constraints. Could not create constraint or index. See previous
errors.
我的两个 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; }
}
和
[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; }
}
我在单元组 class 上只有 Parents 属性,因此我可以轻松访问该单元组中的注册列表。我试图删除 属性 以查看它是否清除了 warning/error,但它没有。有人可以发现我的模型哪里出了问题吗?
此错误表明您不能引入从 table ParentEnrollment
到 table CellGroup
的外键并启用了级联删除,因为这将创建多个级联路径,这在 SQL 服务器上是不允许的。
根据您发布的代码,table 都与 table Parent
以及 AcademicYear
相关,它们位于不可为空的 FK 列上,因此默认情况下,EF 将在删除时启用级联。使用从 ParentEnrollment
到 CellGroup
的另一个 FK,将有多个级联路径,例如Parent
到 CellGroup
到 ParentEnrollment
和 Parent
到 ParentEnrollment
,这导致了你的错误。删除 Parent
属性 不会解决这个问题,因为从 table AcademicYear
.
开始仍然存在相同的级联路径问题
因此您必须禁用外键的级联删除,这必须在您的 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);
}
我有两个 class 相互关联 (one-to-many),我认为我的属性设置正确,但是当我 运行 Update-Database
命令我的迁移,我收到以下错误:
Introducing FOREIGN KEY constraint 'FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID' on table 'ParentEnrollment' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
我的两个 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; }
}
和
[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; }
}
我在单元组 class 上只有 Parents 属性,因此我可以轻松访问该单元组中的注册列表。我试图删除 属性 以查看它是否清除了 warning/error,但它没有。有人可以发现我的模型哪里出了问题吗?
此错误表明您不能引入从 table ParentEnrollment
到 table CellGroup
的外键并启用了级联删除,因为这将创建多个级联路径,这在 SQL 服务器上是不允许的。
根据您发布的代码,table 都与 table Parent
以及 AcademicYear
相关,它们位于不可为空的 FK 列上,因此默认情况下,EF 将在删除时启用级联。使用从 ParentEnrollment
到 CellGroup
的另一个 FK,将有多个级联路径,例如Parent
到 CellGroup
到 ParentEnrollment
和 Parent
到 ParentEnrollment
,这导致了你的错误。删除 Parent
属性 不会解决这个问题,因为从 table AcademicYear
.
因此您必须禁用外键的级联删除,这必须在您的 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);
}