我有一个包含文件夹和集的类文件夹:
public class Folder
{
[Required]
[Key]
public virtual int FolderId { get; set; }
[ForeignKey("ParentFolderId")]
public virtual Folder ParentFolder { get; set; }
public virtual int? ParentFolderId { get; set; }
[ForeignKey("FolderId")]
public virtual ICollection<Set> Sets { get; set; }
[ForeignKey("ParentFolderId")]
public virtual ICollection<Folder> Folders { get; set; }
}
要删除文件夹,我正在这样做:
private void InternalDeleteFolder(DbContext db, int folderId)
{
var folder = InternalGetFolder(db, folderId);
foreach (var subFolder in folder.Folders.ToList())
{
InternalDeleteFolder(db, subFolder.FolderId);
}
foreach (var set in folder.Sets.ToList())
{
InternalDeleteSet(db, set.SetId);
}
if (folder.ParentFolder != null)
{
folder.ParentFolder.Folders.Remove(folder);
SaveChanges(db);
}
db.Folders.Remove(folder);
SaveChanges(db);
}
不过,对于具有大量数据的文件夹来说,这需要很长时间——我必须对每个文件夹/集的数据库进行一次调用。有没有更好的方法来处理这个问题,也许在我的数据库上下文中使用流利的 api?