更新迁移到数据库时出错:外键约束可能导致循环或多个级联路径
Getting Error when updating Migration in to database : Foreign key constraint may cause cycles or multiple cascade paths
这个问题很容易重现,但我不知道正确的解决方法。
类:
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
}
public class Company : IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
public IList<Employee> Employees { get; set; }
}
我正在为用户 table 使用内置身份 ApplicationUser
class。
生成迁移时我没有收到任何类型的错误,但每当我尝试更新数据库时,我都会收到错误消息:
Introducing FOREIGN KEY constraint on table 'Employee' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
使用 Fluent API 解决此问题的正确方法是什么?
项目类型:ASP.NET核心 MVC
第一道工序:-
首先将您的迁移文件 (cascadeDelete: true)
转换为 (cascadeDelete: false)
如果仍然无法正常工作则继续。
使您的外键属性可为空,这应该有效。
像下面这样更新您的代码:-
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
[ForeignKey("ApplicationUserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
public Guid? ApplicationUserId { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
public Guid? CompanyId { get; set; }
}
如果不行再尝试第二个过程:-
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
}
将此添加到您的 DbContext class:-
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
base.OnModelCreating(modelbuilder);
modelbuilder.Entity<Employee>()
.HasOne(b => b.ApplicationUser )
.WithMany(ba => ba.Employee)
.HasForeignKey(bi => bi.ApplicationUserId );
//For Company
modelbuilder.Entity<Employee>()
.HasOne(b => b.Company )
.WithMany(ba => ba.Employee)
.HasForeignKey(bi => bi.CompanyId);
}
第三个过程:-
另一种选择是通过添加此 (EF6) 来删除所有 CASCADE DELETES :-
modelbuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelbuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
希望它能解决您的问题。
这个问题的解决方案是:
only have to configure one FK as non-cascading, that's all. Migration
files shouldn't be altered and the nature relationships shouldn't be
changed by making keys nullable. - @gert-arnold
这个问题很容易重现,但我不知道正确的解决方法。
类:
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
}
public class Company : IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
public IList<Employee> Employees { get; set; }
}
我正在为用户 table 使用内置身份 ApplicationUser
class。
生成迁移时我没有收到任何类型的错误,但每当我尝试更新数据库时,我都会收到错误消息:
Introducing FOREIGN KEY constraint on table 'Employee' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
使用 Fluent API 解决此问题的正确方法是什么?
项目类型:ASP.NET核心 MVC
第一道工序:-
首先将您的迁移文件 (cascadeDelete: true)
转换为 (cascadeDelete: false)
如果仍然无法正常工作则继续。
使您的外键属性可为空,这应该有效。
像下面这样更新您的代码:-
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
[ForeignKey("ApplicationUserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
public Guid? ApplicationUserId { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
public Guid? CompanyId { get; set; }
}
如果不行再尝试第二个过程:-
public class Employee : IEntity<Guid>
{
public Guid Id { get; set; }
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Guid CompanyId { get; set; }
public Company Company { get; set; }
}
将此添加到您的 DbContext class:-
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
base.OnModelCreating(modelbuilder);
modelbuilder.Entity<Employee>()
.HasOne(b => b.ApplicationUser )
.WithMany(ba => ba.Employee)
.HasForeignKey(bi => bi.ApplicationUserId );
//For Company
modelbuilder.Entity<Employee>()
.HasOne(b => b.Company )
.WithMany(ba => ba.Employee)
.HasForeignKey(bi => bi.CompanyId);
}
第三个过程:-
另一种选择是通过添加此 (EF6) 来删除所有 CASCADE DELETES :-
modelbuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelbuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
希望它能解决您的问题。
这个问题的解决方案是:
only have to configure one FK as non-cascading, that's all. Migration files shouldn't be altered and the nature relationships shouldn't be changed by making keys nullable. - @gert-arnold