【发布时间】:2021-05-17 22:59:03
【问题描述】:
假设我们在 EF Core 中有以下多对多关系场景,并具有以下导航属性:
public class Request{
public HashSet<User> Users { get; set; }
}
public class User{
public HashSet<Request> Requests { get; set; }
}
一个用户可能被简单地分配给多个请求,一个请求可能有多个用户。
现在,我想扩展功能,以便每个用户都能够标记分配给他的请求:
public class User{
public HashSet<Request> Requests { get; set; }
public Dictionary<long, bool> RequestsFlags { get; set; } //Request ID (long) and Flag (bool)
}
这样我可以检查设置了标志的请求并相应地更新 UI。问题是这两个集合当然必须始终相互同步。
有什么办法可以保证自定义集合始终符合导航属性?
【问题讨论】:
-
docs.microsoft.com/en-us/ef/core/modeling/…。 “附加数据可以存储在连接实体类型中,但为此最好创建一个定制的 CLR 类型。”
标签: c# entity-framework collections properties entity-relationship