【问题标题】:Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE ... Error在表表上引入 FOREIGN KEY 约束键可能会导致循环或多个级联路径。指定 ON DELETE ... 错误
【发布时间】:2023-03-27 17:28:01
【问题描述】:

我试图在 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; }   

    }

这意味着一个团队有许多代理,还有另一个名为 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; }        
    }

现在我想要一个可以添加团队和代理之间关系的类,即本质上我希望能够为每个团队分配一个团队负责人,所以我在下面设置了一个类

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; }
    }

在运行“更新数据库命令”时,我收到 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. 的错误

所以我把模型改成

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; }
}

这导致了这个错误

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.

请帮忙。

【问题讨论】:

    标签: c# entity-framework model-view-controller asp.net-mvc-5


    【解决方案1】:

    您应该禁用 OneToManyCascadeDeleteConvention 以强制 EF 不使用级联删除。在 DbContext 中添加:

    ...
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
    ...
    

    或者你可以让外键为空:

    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; }
    }
    

    取决于你喜欢哪种行为。

    您也可以使用流畅的 API:

    ...
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TeamToLeader>().HasRequired(i => i.Agent).WithMany().WillCascadeOnDelete(false);
    }
    ...
    

    请注意,您的模型 Team 有许多 SalesAgent 和许多 TeamToLeaderTeamSalesAgent 模型中应该有 TeamToLeaders 集合:

    ...
    public virtual ICollection<TeamToLeader> TeamToLeaders { get; set; }
    ...
    

    我不确定您是否需要 Team 与许多 SalesAgent 关系。

    【讨论】:

      【解决方案2】:

      正如this linkthis link 所说...

      理论上是正确的,但 SQL 服务器(不是实体框架)不喜欢它,因为您的模型允许单个员工同时成为第一和第二团队的成员。如果团队被删除,这将导致多个删除路径指向同一个 Employee 实体。

      SQL 服务器不允许同一实体有多个删除路径。

      This link表示可以通过禁用OneToManyCascadeDeleteConventionManyToManyCascadeDeleteConvention来解决,但是那些删除操作应该通过代码手动完成。

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 2018-08-08
        • 2011-05-08
        • 2013-10-22
        • 2020-12-20
        • 2021-05-30
        相关资源
        最近更新 更多