0

i was trying to run the Update-Database command in Nugget Package Manager console but wasnt successful as i kept getting the 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..
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

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 i.e 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.

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.

py3r3str
  • 1,879
  • 18
  • 23
ibnhamza
  • 861
  • 1
  • 15
  • 29

2 Answers2

2

You should diasble OneToManyCascadeDeleteConvention to force EF not to use cascade delete. In DbContext add:

...
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:

...
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. There should be TeamToLeaders collection in your Team and SalesAgent model :

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

I'm not sure if you need Team to many SalesAgent relation anymore.

py3r3str
  • 1,879
  • 18
  • 23
1

As this link, and this link 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. If the Team is deleted this will cause multiple delete paths to the same Employee entity.

SQL server doesn't allow multiple delete paths to the same entity.

This link said that it can be solved by disabling OneToManyCascadeDeleteConvention and ManyToManyCascadeDeleteConvention, but those deleting operations SHOULD BE done by codes manually.

Community
  • 1
  • 1
AechoLiu
  • 17,522
  • 9
  • 100
  • 118