Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Introducing FOREIGN KEY constraint 'FK_PMS_UserRoles_PMS_Users_UserId' on table 'PMS_UserRoles' 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 or index. See previous errors.

What I have tried:

public class PMS_Company : IBaseEntity
    {
        public PMS_Company()
        {
            Users = new HashSet<PMS_User>();
            Roles = new HashSet<PMS_Role>();
          
        }
       
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string RegistrationNo { get; set; }
        public string OwnerName { get; set; }
        public string Logo { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public bool IsActive { get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public int? EditedBy { get; set; }
        public DateTime? EditedOn { get; set; }
 
 
        public IReadOnlyCollection<PMS_User> Users { get; set; }
        public IReadOnlyCollection<PMS_Role> Roles { get; set; }
       
    }
public class PMS_User : IBaseEntity
    {
        public PMS_User()
        {
          UserRoles = new HashSet<PMS_UserRole>();
        }
        public int Id { get ; set ; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Paswword { get; set; }
        public string salt { get; set; }
        public DateTime ExpireDate { get; set; }
        public bool IsActive { get ; set ; }
        public int CreatedBy { get ; set ; }
        public DateTime CreatedOn { get ; set ; }
        public int? EditedBy { get ; set ; }
        public DateTime? EditedOn { get ; set ; }
 
        public int CompanyId { get; set; }
        public PMS_Company Company { get; set; }
 
        public IReadOnlyCollection<PMS_UserRole> UserRoles { get; set; }
    }
 
  public class PMS_UserRole 
    {
        
        public int UserId { get; set; }
        public PMS_User User { get; set; }
        public int RoleId { get; set; }
        public PMS_Role Role { get; set; }
    }
 
  public class PMS_Role : IBaseEntity
    {
        public PMS_Role()
        {
           UserRoles = new HashSet<PMS_UserRole>();
        }
        public int Id { get ; set; }
        public string RoleName { get; set; }
        public string RoleNameN { get; set; }
        public bool IsActive { get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public int? EditedBy { get; set; }
        public DateTime? EditedOn { get; set; }
 
        public int CompanyId { get; set; }  
        public PMS_Company Company { get; set; }
 
       public IReadOnlyCollection<PMS_UserRole> UserRoles { get; set; }
        
    }
 
 public class UserRolesConfiguration : IEntityTypeConfiguration<PMS_UserRole>
    {
        public void Configure(EntityTypeBuilder<PMS_UserRole> builder)
        {
            builder.ToTable("PMS_UserRoles");
 
            builder.HasKey(x => new { x.RoleId, x.UserId });
            builder.HasOne(ur => ur.Role)
                .WithMany(x => x.UserRoles)
                .HasForeignKey(x => x.RoleId);
            builder.HasOne(ur => ur.User)
                .WithMany(x => x.UserRoles)
                .HasForeignKey(x => x.UserId);
        }
    }
Posted
Updated 27-Aug-19 8:07am

Quote:
Introducing FOREIGN KEY constraint 'FK_PMS_UserRoles_PMS_Users_UserId' on table 'PMS_UserRoles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
The error itself is explaining itself, the FOREIGN KEY being created in this migration is likely to cause a cycle; connection in the tables.

It is a good approach to re-evaluate the database schema, and see how this is being caused. You can try reading documentation for these causes to understand what they are, like here: .net - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? - Stack Overflow[^]
 
Share this answer
 
The key is is the phrase "multiple cascade paths". This means that deleting one entity could cause another entity to be deleted via multiple relationships.

In this case, deleting a PMS_Company will delete both the users and the roles in that company. Deleting the users will delete the associated PMS_UserRole entities. Deleting the roles will delete the associated PMS_UserRole entities. Therefore, there are two paths by which a single delete could cause a PMS_UserRole to be deleted, which SQL Server does not allow.

Error message 1785 occurs when you create a FOREIGN KEY constraint that may cause multiple cascade paths[^]

It's an annoying limitation, but it's one you have to live with. You will have to change one or more of the relationships to not cascade on delete. When you want to delete the parent entity, you will need to manually delete the related entities instead of relying on the cascade delete behaviour.

For example:
C#
builder.HasOne(ur => ur.Role)
    .WithMany(x => x.UserRoles)
    .HasForeignKey(x => x.RoleId)
    .OnDelete(DeleteBehavior.None);
Cascade Delete - EF Core | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900