我试图为我们的数据库设置 CacadeOnDelete,这样如果您删除 a Folder
,所有子文件夹也会被删除。
Folder
是这样设置的
public class Folder
{
[Key]
public int FolderId { get; set; }
public string Name { get; set; }
public Folder Parent { get; set; }
[ForeignKey("Parent")]
public int ParentFolderId { get; set; }
public virtual ICollection<Folder> Children { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
而且我试图像这样设置级联删除,但是当我运行它时,数据库生成永远不会完成,尽管我也没有例外
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Folder>()
.HasMany(x => x.Devices)
.WithMany(x => x.PresentInFolders);
modelBuilder.Entity<Folder>()
.HasOptional(f => f.Parent)
.WithMany(f => f.Children)
.HasForeignKey(f => f.ParentFolderId)
.WillCascadeOnDelete();
}