Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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".

like image 778
Patrice Calvé Avatar asked Dec 05 '22 12:12

Patrice Calvé


1 Answers

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);

like image 133
RaghuRam Nadiminti Avatar answered May 25 '23 04:05

RaghuRam Nadiminti