Make EF use a m:n table with additional property [duplicate]

落爺英雄遲暮 提交于 2019-12-20 03:21:51

问题


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.

So there are the following Models:

Entry:

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

Hardware:

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

OrderStatus:

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

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.

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?


回答1:


As per this answer:

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.



来源:https://stackoverflow.com/questions/40019589/make-ef-use-a-mn-table-with-additional-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!