如何在实体框架中使用属性建模 to m 关系而不添加额外的表

发布于 2024-11-30 03:06:18 字数 432 浏览 0 评论 0原文

我对实体框架还很陌生,我正在对这个简单的结构进行建模:

在此处输入图像描述

使用此模型可以做什么我有一个具有 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-12-07 03:06:18

不直接。 EF 只能按照您当前看到的方式映射关系,但您可以将自定义行为添加到实体的部分部分。简单的方法是这样的:

public partial class Users
{
    public IEnumerable<PrioritizedGroup> Groups 
    { 
        get
        {
            return UserGroups.Select(ug => new PrioritizedGroup
                                                   {
                                                        Priority = ug.Priority,
                                                        Id = ug.Group.Id,
                                                        Name = ug.Group.Name,
                                                        Description = ug.Group.Description 
                                                   })
                             .OrderBy(g => g.Priority);
        }  
    }
}

要直接在 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

public partial class Users
{
    public IEnumerable<PrioritizedGroup> Groups 
    { 
        get
        {
            return UserGroups.Select(ug => new PrioritizedGroup
                                                   {
                                                        Priority = ug.Priority,
                                                        Id = ug.Group.Id,
                                                        Name = ug.Group.Name,
                                                        Description = ug.Group.Description 
                                                   })
                             .OrderBy(g => g.Priority);
        }  
    }
}

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 by ObservableCollection back to original UserGroups collection. Once you have something like that implemented you can hide original collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文