如何在实体框架中使用属性建模 to m 关系而不添加额外的表
我对实体框架还很陌生,我正在对这个简单的结构进行建模:
使用此模型可以做什么我有一个具有 UsersGroups 属性(UserGroups 对象的集合)的 Users 类。
我想要一个 Users 类,其属性类似于 Tuple 类型的 Groups 或类似的属性(一个新的 PriorizedGroup 类等),与业务更相关。
实体框架可以做到这一点吗?
提前致谢。
编辑:如果我对bussines对象进行建模,我将创建一个带有Groups属性的User类,该属性包含用户所属的所有组,并带有一个额外的属性来存储其优先级(使用元组,使用继承的类,如您所愿)。问题是我觉得Entity框架创建的对象类似于SQL结构,而不是业务结构。
I'm pretty new to the Entity framework and I'm modelling this simple structure:
With this model what I have is a Users class with a property UsersGroups (a collection of UserGroups objects).
I would like to have a Users class with a property like Groups with type Tuple or something like this (a new PriorizedGroup class, etc) that is much more related with the bussines.
Is this possible with the Entity framework?
Thanks in advance.
EDIT: If I were modeling the bussines objects I would create a User class with a Groups property that contained all the groups the user pertains with an extra property to store its priority (with a tuple, with an inherited class, as you wish). The thing is that I feel that the objects created by the Entity framework resemble the SQL structure, not the business structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不直接。 EF 只能按照您当前看到的方式映射关系,但您可以将自定义行为添加到实体的部分部分。简单的方法是这样的:
要直接在 EF 中实现此操作,您需要一些高级映射技术,这将要求您直接修改 EDMX 源代码(DefiningQuery 或 QueryView),并且它将使实体只读(您将需要存储过程修改)。
要使在 Users 上公开的集合可更新,您可能需要使用
ObservableCollection
并将ObservableCollection
触发的所有修改传输回原始UserGroups
集合。一旦你实现了类似的东西你可以隐藏原始集合。Not directly. EF can map the relation only in the way you see it at the moment but you can add your custom behavior to your partial part of the entity. The simple way is something like
To make this happen directly in EF you need some advanced mapping technique which will require you to modify EDMX source code directly (either DefiningQuery or QueryView) and it will make the entity read only (you will need stored procedures for modification).
To make the collection exposed on Users updatable you would probably need to use
ObservableCollection
and transfer all modifications triggered byObservableCollection
back to originalUserGroups
collection. Once you have something like that implemented you can hide original collection.