繁体   English   中英

EF Core 中的条件导航属性

[英]Conditional navigation properties in EF Core

我有一个 User 表,其中有一个相应的 UserAudits 表来跟踪更改。 操作类型存储在审计表中(插入、更新、删除)。

我想在我的用户实体上创建一个导航属性,以便我可以从审计表中获取“插入”记录。 这可能吗? 通常在设置导航属性时,无法在连接上指定额外条件。

用户:

public class User
{
    public int Id {get; set;}
    public string Name {get;set;}
    public UserAudit InsertedAuditRecord {get; set;} // <-- How to configure this?
}

用户审核:

public class UserAudit
{
    public int AuditId {get; set;
    public int Id {get; set;}
    public string Name {get; set;}
    public string OperationType {get; set;}
}

我知道我可以在我的用户实体上创建一个一对多的导航属性,然后查询:

ICollection<UserAudit> UserAudits {get; set;}
UserAudit InsertedAuditRecord => UserAudits.First(ua => ua.OperationType = "insert");

但这需要将用户的整个审计表带入 memory 以查找单个记录。

当然有一些方法可以做到这一点?

我猜您正在寻找的内容(其中包含使用Where()子句的Include() )现在已添加到.net 5中。 看看这个答案。

子记录上的Where ( First ) 不会将整个子 object 带回来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM