2

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?

Th1sD0t
  • 1,089
  • 3
  • 11
  • 37
  • I think you should create a third entity, **EntryHardwareStatus** (name could probably be better, but that's irrelevant for now), which is related to both Entry and Hardware and also contains OrderStatus. Just mapping the `m:n` relationship with fluent api is not enough – Jakub Jankowski Oct 13 '16 at 11:27
  • So there's no other way than creating a new Model? Don't I have to create new properties in the Context + new Repository for this Model? Isn't there a way to solve this with fluent-api ? – Th1sD0t Oct 13 '16 at 11:31

1 Answers1

2

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.

Community
  • 1
  • 1
Jakub Jankowski
  • 731
  • 2
  • 9
  • 20