使EF使用具有附加属性的m:n表 [英] Make EF use a m:n table with additional property

查看:96
本文介绍了使EF使用具有附加属性的m:n表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新应用时遇到问题。该应用程序是关于在特定日期进入的新员工的。由于他们需要一些硬件(例如笔记本电脑,键盘等),因此我们希望提供有关未来几天将要出现的所有新员工的概述。

I ran into a problem when creating a new app. The app is about new employees that enter on a specific date. As they need some Hardware (such as Notebooks, keyboards etc.) we want to provide an overview over all the new employees that are going to come in the next few days.

因此有以下模型:

条目:

public class Entry{
public IEnumerable<Hardware> Hardware {get; set;}
...
}

硬件:

public class Hardware{
    public string Name {get; set;}
    public OrderStatus Status {get; set}
}

OrderStatus:

OrderStatus:

public enum OrderStatus{
    Nothing,
    Ordered,
    Arrived,
    Ready
}

起初这看起来不错,因为我想为每个条目创建一个新的硬件数据集(一对多)。现在,我决定维护一个硬件列表,仅将硬件引用到该条目(很多到很多)。问题是OrderStatus绑定到硬件,因此当一个硬件引用到许多员工并且Orderstatus更改时,它将在所有条目上更改。

At first this seemed fine because for every entry I wanted to create a new Hardware dataset (one to many). Now I decided to maintain a list of Hardware and just reference the Hardware to the Entry (many to many). The problem is that the OrderStatus is bound to the Hardware so when one Hardware is referenced to many Employees and the Orderstatus gets changed it will be changed on all entries.

我认为正确的解决方案是使用第三个表(Entry_Id,Hardware_Id,OrderStatus)。使用流畅的API,可以在新的(第三个)表中映射关系,但无法向其中添加新的属性(OrderStatus)。

I think the proper solution would be to use a third table (Entry_Id, Hardware_Id, OrderStatus). With fluent API it's possible to map a relationship in a new (third) table but it's not possible to add a new property (OrderStatus) to it. Is this even possible?

推荐答案

根据此答案


使用自定义的
不能创建多对多关系联接表。在多对多关系中,EF内部管理和隐藏
连接表。该表格在您的模型中没有实体类
。要使用具有其他
属性的联接表,您实际上必须创建两个一对多的
关系

It's not possible to create a many-to-many relationship with a customized join table. In a many-to-many relationship EF manages the join table internally and hidden. It's a table without an Entity class in your model. To work with such a join table with additional properties you will have to create actually two one-to-many relationships

有关更多详细信息,请参阅链接的答案。

Please refer to the linked answer for more details.

这篇关于使EF使用具有附加属性的m:n表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆