AspNet Core WebApi OData - Many to Many join tables

asp.net-core entity-framework-core odata relationship

Question

As is the recommended pattern in EF Core these days when you have a many to many join we do something like this:

Fluent API, many-to-many in Entity Framework Core

Having done that I'm faced with the issue as to how I go about exposing that in the OData model.

Since technically the Entity type definition has no key property (as it uses a composite key the OData framework doesn't like me adding the set to the model.

Whats the recommended approach to this problem?

1
0
7/16/2019 10:08:59 PM

Accepted Answer

It seems that EF and OData have become somewhat synced up, what would be even better would be if they could literally share model building code.

To that end I found that in the OData model build after calling AddSet I was able to define the key in the same way as I did in EF ...

Builder.EntityType<UserRole>().HasKey(ac => new { ac.UserId, ac.RoleId });

This is somewhat clean, I have not yet tried posting or directly requesting such a type yet, but expanding from either side of the relationship chain seems to work fine.

0
7/17/2019 8:08:31 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow