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".
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,
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'.
Mark the key property(or properties) on the type 'Order' with the [Key]
attribute.
If you hate attributes and prefer doing it in code, you can do, builder.EntitySet<Order>("orders").EntityType.HasKey(o => order.KeyProperty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With