© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
Alerin

Error Introducing FOREIGN KEY constraint

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Introducing FOREIGN KEY constraint 'FK_Identity.Roles_Identity.User_UserId' on table 'Identity.Roles' 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.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Introducing FOREIGN KEY constraint 'FK_Identity.Roles_Identity.User_UserId' on table 'Identity.Roles' 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.


Models:
public class Roles
{
    public int Id { get; set; }
    public User User { get; set; } = null!;
    public Guid UserId { get; set; }
    public Role Role { get; set; } = null!;
    public int RoleId { get; set; }
}
public class Roles
{
    public int Id { get; set; }
    public User User { get; set; } = null!;
    public Guid UserId { get; set; }
    public Role Role { get; set; } = null!;
    public int RoleId { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}
public class Role
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}

public class RolePolicy
{
    public int Id { get; set; }
    public Role Role { get; set; } = null!;
    public int RoleId { get; set; }
    public string Policy { get; set; } = string.Empty;
}
public class RolePolicy
{
    public int Id { get; set; }
    public Role Role { get; set; } = null!;
    public int RoleId { get; set; }
    public string Policy { get; set; } = string.Empty;
}

public class User
{
    [Key]
    public Guid Id { get; set; }

    [StringLength(16, MinimumLength = 4), Required]
    public string Name { get; set; } = string.Empty;                                   

    ...

    public Role DisplayGroup { get; set; } = null!;                                     
    public int DisplayGroupId { get; set; }
    public ICollection<Roles> Roles { get; set; } = new List<Roles>(); 
public class User
{
    [Key]
    public Guid Id { get; set; }

    [StringLength(16, MinimumLength = 4), Required]
    public string Name { get; set; } = string.Empty;                                   

    ...

    public Role DisplayGroup { get; set; } = null!;                                     
    public int DisplayGroupId { get; set; }
    public ICollection<Roles> Roles { get; set; } = new List<Roles>(); 
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Entity Framework Core One-to-One Relationship => Introducing FOREIGN KEY constraint
C#CC# / help
3y ago
Entity Framework SQL Error 19 - Foreign Key Constraint Failed
C#CC# / help
4y ago
Entity Framework Core 8 -Foreign Key Constraint failure.
C#CC# / help
2y ago
❔ Troubleshooting EF Core: Error Message - INSERT Statement Conflicts with FOREIGN KEY Constraint
C#CC# / help
3y ago