且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF代码首先级联删除外键一对多

更新时间:2023-02-02 22:46:29

WithRequired 介绍了2路关系。所以你应该执行以下操作。

The WithRequired introduces the 2 way relationship. So you should do the following.

modelBuilder
    .Entity<Video>()
    .HasMany(v => v.Imgages)
    .WithOptional()
    .WillCascadeOnDelete(true);

...假设你想要的关系是相反的...

... and assuming you want the relationship the other way around ...

public class Video { }
public class ImageInfo {
    public virtual Video { get; set; }
}

modelBuilder
    .Entity<ImageInfo>()
    .HasRequired(v => v.Video)
    .WithMany()
    .WillCascadeOnDelete(true);

PS:我认为列表< ImageInfo> 应该是 virtual ,所以这里是我如何定义它...

PS: I think the List<ImageInfo> should be virtual, so here is how I'd define it ...

public class Video {
    public Video() { this.Images = new List<ImageInfo>(); }
    public virtual ICollection<ImageInfo> Images { get; set; }
}