【发布时间】:2021-09-05 03:12:09
【问题描述】:
我遇到了多对多关系的问题,我不明白为什么会这样:
所以我有以下内容:
public class A
{
public Guid Guid { get; set; }
public ICollection<AB> ABs { get; set; }
}
public class B
{
public Guid Guid { get; set; }
public ICollection<AB> ABs { get; set; }
}
public class AB
{
public Guid Guid { get; set; }
public A A { get; set; }
public Guid A_Id { get; set; }
public B B { get; set; }
public Guid B_Id { get; set; }
internal static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<AB>(entity =>
{
entity.HasIndex(e => new { e.A_Id, e.B_Id}).IsUnique();
entity.HasOne(e => e.A)
.WithMany(p => p.ABs)
.HasForeignKey(e => e.A_Id)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(e => e.B)
.WithMany(w => w.ABs)
.HasForeignKey(e => e.B_Id)
.OnDelete(DeleteBehavior.Restrict);
});
}
}
我不断得到:
实体类型“A”和“AB”之间的关联已被切断,但 该关系要么标记为“必需”,要么隐含 需要,因为外键不可为空。如果 当需要关系时,应删除依赖/子实体 被切断,然后设置关系以使用级联删除。 考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来 查看关键值。
问题是我想通过从 A.AB 中删除 AB 来打破关系,而 EF Core 试图删除 A。我不明白发生了什么。有什么想法吗?
最好的问候
【问题讨论】:
标签: .net-core entity-framework-core ef-core-2.2