I have the following relationship defined, where both Customer
and Retailer
are foreign keys to a User
. So a User
can be either a Customer
, Retailer
or both. Each Customer
can have a list of Retailer
s, and each Retailer
can have a list of Customer
s.
The relationship is defined as:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Retailer>()
.HasRequired(x => x.User);
modelBuilder.Entity<Customer>()
.HasRequired(x => x.User);
}
with the entities defined as follows:
public class Retailer : Entity
{
public string Name { get; set; }
public virtual User User { get; set; }
public ICollection<Customer> Customers { get; set; }
}
public class Customer : Entity
{
public string Name { get; set; }
public virtual User User { get; set; }
public ICollection<Retailer> Retailers { get; set; }
}
public class User : AbpUser<Tenant, User>
{
public string AccessToken { get; set; }
public long UserId { get; set; }
public virtual Retailer Retailer { get; set; }
public virtual Customer Customer { get; set; }
}
When I try to delete a User
that has a Customer
or Retailer
foreign key, I get the error (either depending on the type of foreign key):
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Customers_dbo.AbpUsers_User_Id". The conflict occurred in database "ShopDB", table "dbo.Customers", column 'User_Id'.
or
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Retailers_dbo.AbpUsers_User_Id". The conflict occurred in database "ShopDB", table "dbo.Retailers", column 'User_Id'.
What do I need to change so that when I delete a User
all foreign referenes to the User
are also cascade deleted?