I want to create a user relationship table in ASP.NET Core and encounter some problems. If I have to disable cascade delete because of this, how do I prevent orphans?
Error:
Introducing FOREIGN KEY constraint 'FK_UserRelationships_AspNetUsers_User2Id' on table 'UserRelationships' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
ApplicationDbContext.cs:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserRelationship>().HasKey(x => new { x.User1Id, x.User2Id });
}
public DbSet<UserRelationship> UserRelationships { get; set; }
}
My current model:
public class UserRelationship
{
public byte RelationshipType { get; set; }
public ApplicationUser User1 { get; set; }
public string User1Id { get; set; }
public ApplicationUser User2 { get; set; }
public string User2Id { get; set; }
}