简体   繁体   English

在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误

[英]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 : 您的TeamSalesAgent模型中应该有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. 该链接表示可以通过禁用OneToManyCascadeDeleteConventionManyToManyCascadeDeleteConvention来解决,但这些删除操作应由代码手动完成。

暂无
暂无

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

相关问题 在表 'XY' 上引入 FOREIGN KEY 约束 'FK_XY' 可能会导致循环或多个级联路径。 指定 ON DELETE NO ACTION - Introducing FOREIGN KEY constraint 'FK_XY' on table 'XY' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 表&#39;UsageSummaries&#39;上的多态与引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Polymorphism vs Introducing FOREIGN KEY constraint '' on table 'UsageSummaries' may cause cycles or multiple cascade paths FOREIGN KEY 约束可能会导致循环或多个级联路径。 错误 - FOREIGN KEY constraint may cause cycles or multiple cascade paths. error 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么? - Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Error: introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM