4
votes

I'm getting started with OData and Entity Framework.

I created a Controller that exposes Customer. In the model (edmx) there's only one Entity (Customer) and everything works fine (data is being rendered).

Now, when I add a second entity (Order) (Update model from database, select Order), the 1 to many (1 Customer to Many Order) gets setup automatically.

I do nothing else and when I compile/run the controller, I get this error:

Line 23:         builder.EntitySet(Of Customer)("Customers")
Line 24:         Return builder.GetEdmModel()  --> Exception Here
Line 25: 
Line 26:     End Function

The complex type 'WebTools.Order' refers to the entity type 'WebTools.Customer' through the property 'Customer'.

If I remove the Order Entity, it works.

I'm not sure if the problem is with the "configuration" of the OData/WebAPI part of the equation or the "Entity Framework".

1

1 Answers

16
votes

I assume the model builder is not able to figure out the key property for the entity type Order. You can help out the model builder through a couple of ways,

  1. builder.EntitySet<Order>("orders");. This adds a new entityset 'orders' and also has the effect of marking the type 'Order' as an entity type. You also have to specify the key property of 'Order'.

  2. Mark the key property(or properties) on the type 'Order' with the [Key] attribute.

  3. If you hate attributes and prefer doing it in code, you can do, builder.EntitySet<Order>("orders").EntityType.HasKey(o => order.KeyProperty);