有我的简单模型:
class User
{
public ICollection<BusinessCard> BusinessCards { get; set; }
public ICollection<BusinessCardExtension> BusinessCardsForMe { get; set; }
}
class BusinessCard
{
public ApplicationUser User { get; set; }
public string UserId { get; set; }
public ICollection<BusinessCardExtension> ForUsers { get; set; }
}
//it is the class for introducing of many-to-many relationship:
class BusinessCardExtension
{
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public string BusinessCardID { get; set; }
public BusinessCard BusinessCard { get; set; }
}
我需要User和BusinessCard之间的一些关系:一个是一对多,另一个是多对多。从概念上讲,他们是不同的关系。
由于从User到BusinessCardExtension有双重级联路径,我将如下配置ModelBuilder:
//just configure many-to-many...
builder.Entity<BusinessCardExtension>()
.HasKey(b => new { b.UserId, b.BusinessCardID });
builder.Entity<BusinessCardExtension>()
.HasOne(b => b.BusinessCard)
.WithMany(c => c.ForUsers)
.HasForeignKey(b => b.BusinessCardID);
builder.Entity<BusinessCardExtension>()
.HasOne(b => b.User)
.WithMany(u => u.BusinessCardsForMe)
.HasForeignKey(b => b.UserId);
//then to disable double cascade deletion:
builder.Entity<BusinessCard>()
.HasOne(c => c.User)
.WithMany(u => u.BusinessCards)
.HasForeignKey(c => c.UserId)
.OnDelete(DeleteBehavior.Restrict);
然而它没有帮助。 Shell返回错误:
引入FOREIGN KEY约束 &#39; FK_BusinessCardExtension_Users_UserId&#39;桌子上 &#39; BusinessCardExtension&#39;可能会导致循环或多个级联路径。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他 FOREIGN KEY约束。