Reputation: 3361
I have the following entities:
public class CompanyRelationshipEntity
{
public int ParentCompanyId { get; set; }
public int ChildCompanyId { get; set; }
public CompanyEntity? ParentCompany { get; set; }
public CompanyEntity? ChildCompany { get; set; }
}
And
public class CompanyEntity
{
public int Id { get; set; }
public CompanyRelationshipEntity? ChildCompany{ get; set; }
public CompanyRelationshipEntity? ParentCompany { get; set; }
}
With the following relationship defined on the CompanyRelationshipTable:
model.ToTable("CompanyRelationship");
model.HasKey(x => new { x.ParentCompanyId, x.ChildCompanyId });
model.HasOne(e => e.ChildCompany)
.WithOne(e => e.ChildCompany)
.HasForeignKey<CompanyRelationshipEntity>(x => x.ChildCompanyId)
.OnDelete(DeleteBehavior.Cascade);
model.HasOne(e => e.ParentCompany)
.WithOne(e => e.ParentCompany)
.HasForeignKey<CompanyRelationshipEntity>(x => x.ParentCompanyId)
.OnDelete(DeleteBehavior.Cascade);
When trying to create the database I get the following:
Introducing FOREIGN KEY constraint 'FK_CompanyRelationship_Company_ParentCompanyId' on table 'CompanyRelationship' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints
If I change this relationship to ClientCascade, it works. The expected behaviour I want to achieve is to remove the record from the CompanyRelationship table if either the Parent or Child company is deleted but ideally I don't want to have to load this relationship before doing so.
Upvotes: 0
Views: 443