I’m designing a database and I’m stuck trying to figure out what sort of relationship I need.
I have an Order entity which is mapped 1-Many with an OrderItem entity. This works fine. However, I want to find a way to link each OrderItem with exactly one Product but the Product does not need to know about any of the OrderItems.
The problem I get is that when I attempt to add a Product, Entity Framework complains that I have not specified an OrderItem:
Entities in ‘ModelContainer.Products’ participate in the ‘OrderItemProducts’ relationship. 0 related ‘OrderItem’ were found. 1 ‘OrderItem’ is expected.
So, I don’t think I have the correct type of relationship between OrderItem and Product.
My question: Do I even need a relationship here? If so, how can I link one OrderItem to a Product, but not vice-versa? If not, what other approach might I take?
It seems obvious now, but I had a 1-Many relationship between OrderItem and Product but it was in the wrong direction.
When using the model designer in Visual Studio, I needed to click from the Product entity to the OrderItem entity. Now, the direction is correct. The entity that you start from is the entity that becomes the ‘1’ in the ‘1-Many’ relationship.