我在数据库中使用软删除(IsDeleted 场地)。我积极使用 LoadWithAssociateWith 检索和过滤嵌套记录的方法。

事情是 AssociateWith 仅与代表一对多关系的属性合作。

DataLoadOptions loadOptions = new DataLoadOptions();
loadOption.LoadWith<User>(u = > u.Roles);
loadOption.AssociateWith<User>(u = > u.Roles.Where(r = > !r.IsDeleted));

在上面的示例中,我只是说: 我想通过相关(未删除)角色检索用户.

但是当我有一对一的关系时 Document -> File (唯一一个与文档有关的文件)我无法过滤软删除的对象:

DataLoadOptions loadOptions = new DataLoadOptions();
loadOption.LoadWith<Document>(d = > d.File);
// the next certainly won't work
loadOption.AssociateWith<File>(f = > !f.IsDeleted);

那么,有什么想法如何过滤一对一关系中的记录?

谢谢!

有帮助吗?

解决方案

到目前为止,我只找到了一个合适的解决方案(除了完全不使用软删除之外):删除实体更新中的软删除关系。

例如,当我决定从文档中删除文件时,我执行类似的内容:

// this may be a part of update method
var file = document.File;
if (file.IsDeleted)
{
    // attach soft deleted file
    context.Files.Attach(file, true); 

    // remove a file reference
    document.File = null;
}

// attach document entity and submit changes
context.Documents.Attach(document, true);
context.SubmitChanges();

因此,这可能是对复杂数据检索一对一关系过滤的替代。

其他提示

也许尝试:

loadOptions.AssociateWith<File>(f => f.IsDeleted ? null : f);

这将为您提供null而不是iSdeleted的文件为true。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top