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

时间:2015-03-05 13:19:15

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

我试图在Nugget Package Manager控制台中运行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; }
    }

运行&#34;更新 - 数据库命令&#34;我收到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.

请帮助。

2 个答案:

答案 0 :(得分:2)

您应该使用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和许多TeamToLeader。 您的TeamToLeadersTeam模型中应该有SalesAgent个收集:

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

我不确定您是否需要TeamSalesAgent关系。

答案 1 :(得分:1)

作为this linkthis link说...

  

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

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

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