1

我在我的数据库(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);

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

谢谢!

4

2 回答 2

1

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

例如,当我决定从文档中删除文件时,我执行以下操作:

// 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();

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

于 2010-06-21T17:01:14.550 回答
0

也许尝试:

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

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

于 2010-05-21T15:12:57.413 回答