[英]Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error
i was trying to run the Update-Database command in Nugget Package Manager console but wasnt successful as i kept getting the error 我试图在Nugget软件包管理器控制台中运行Update-Database命令,但未成功,因为我不断收到错误
Introducing FOREIGN KEY constraint 'FK_dbo.TeamToLeaders_dbo.Teams_TeamId' on table 'TeamToLeaders' 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. See previous errors..
I want to set up relationship in which there is a class called Team.cs that contains the below properties
public class Team
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public Decimal MonthlyTarget { get; set; }
public ICollection<SalesAgent> Agents { get; set; }
}
which means a team has many Agents and there is another class called SalesAgent.cs which contain info about agents 这意味着一个团队有许多座席,还有另一个名为SalesAgent.cs的类,其中包含有关座席的信息
public class SalesAgent
{
[Key]
public int AgentId { get; set; }
public string AgentFirstName { get; set; }
public string AgentLastName { get; set; }
public string HomeAddress { get; set; }
public bool IsActive { get; set; }
public string AgentPhone { get; set; }
public Decimal MonthlyTarget { get; set; }
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
Now i want a class which i would be able add the relationship between a team and an agent ie in essence i want to be able to assign a team leader to each team so i set up the class below 现在我想要一个班级,我可以在该班级中添加团队与座席之间的关系,即从本质上讲,我希望能够为每个团队分配一个团队负责人,因此我在下面设置了班级
public class TeamToLeader
{
[Key]
public int TeamToLeaderId { get; set; }
[ForeignKey("Team")]
public int TeamId { get; set; }
public int AgentId { get; set; }
public virtual Team Team { get; set; }
[ForeignKey("AgentId")]
public virtual SalesAgent Agent { get; set; }
}
Upon running "Update-Database Command" I get an error that The ForeignKeyAttribute on property 'AgentId' on type 'SalesForce.Models.TeamToLeader' is not valid. The navigation property 'SalesAgent' was not found on the dependent type 'SalesForce.Models.TeamToLeader'. The Name value should be a valid navigation property name.
运行“ Update-Database Command”时,出现错误,即
The ForeignKeyAttribute on property 'AgentId' on type 'SalesForce.Models.TeamToLeader' is not valid. The navigation property 'SalesAgent' was not found on the dependent type 'SalesForce.Models.TeamToLeader'. The Name value should be a valid navigation property name.
The ForeignKeyAttribute on property 'AgentId' on type 'SalesForce.Models.TeamToLeader' is not valid. The navigation property 'SalesAgent' was not found on the dependent type 'SalesForce.Models.TeamToLeader'. The Name value should be a valid navigation property name.
So i changed the model to 所以我将模型更改为
public class TeamToLeader
{
[Key]
public int TeamToLeaderId { get; set; }
[ForeignKey("Team")]
public int TeamId { get; set; }
[ForeignKey("SalesAgent")]
public int AgentId { get; set; }
public virtual Team Team { get; set; }
public virtual SalesAgent Agent { get; set; }
}
and that resulted in this error 并导致此错误
Introducing FOREIGN KEY constraint 'FK_dbo.TeamToLeaders_dbo.Teams_TeamId' on table 'TeamToLeaders' 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. See previous errors.
Help please. 请帮助。
You should diasble OneToManyCascadeDeleteConvention to force EF not to use cascade delete. 您应该让OneToManyCascadeDeleteConvention强制EF不要使用级联删除。 In DbContext add:
在DbContext中添加:
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
...
Or you can make foreign keys nullable: 或者,您可以使外键为空:
public class TeamToLeader
{
[Key]
public int? TeamToLeaderId { get; set; }
[ForeignKey("Team")]
public int? TeamId { get; set; }
[ForeignKey("SalesAgent")]
public int AgentId { get; set; }
public virtual Team Team { get; set; }
public virtual SalesAgent Agent { get; set; }
}
Depends which behavior you prefer. 取决于您喜欢哪种行为。
You can also use fluent API: 您还可以使用流畅的API:
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TeamToLeader>().HasRequired(i => i.Agent).WithMany().WillCascadeOnDelete(false);
}
...
Note that your model Team
has many SalesAgent
and many TeamToLeader
. 请注意,您的模型
Team
有许多SalesAgent
和许多TeamToLeader
。 There should be TeamToLeaders
collection in your Team
and SalesAgent
model : 您的
Team
和SalesAgent
模型中应该有TeamToLeaders
集合:
...
public virtual ICollection<TeamToLeader> TeamToLeaders { get; set; }
...
I'm not sure if you need Team
to many SalesAgent
relation anymore. 我不确定您是否需要
Team
与许多SalesAgent
关系。
As this link , and this link saids... 作为这个链接 ,并且此链接 SAIDS ...
It is theoretically correct but SQL server (not Entity framework) doesn't like it because your model allows single employee to be a member of both First and Second team.
从理论上讲这是正确的,但是SQL Server(不是Entity Framework)不喜欢它,因为您的模型允许单个员工同时成为第一和第二团队的成员。 If the Team is deleted this will cause multiple delete paths to the same
Employee
entity.如果删除团队,这将导致到同一
Employee
实体的多个删除路径。
SQL server doesn't allow multiple delete paths to the same entity. SQL Server不允许到同一实体的多个删除路径。
This link said that it can be solved by disabling OneToManyCascadeDeleteConvention
and ManyToManyCascadeDeleteConvention
, but those deleting operations SHOULD BE done by codes manually. 该链接表示可以通过禁用
OneToManyCascadeDeleteConvention
和ManyToManyCascadeDeleteConvention
来解决,但这些删除操作应由代码手动完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.