Last post May 01, 2014 05:02 AM by Lybax
Mar 27, 2013 05:41 PM|rick.dodd|LINK
All the examples of using OData with Web API are shown with code first. I'm looking for but haven't found a way to use the EDM I created, using DB First, as the basis for creating the OData model. I'm hoping that some other model builder comparable to
ODataConventionModelBuilder exist. Something along the lines of ODataEdmModelBuilder so the code might looks like this:
ODataModelBuilder modelBuilder = new ODataEdmModelBuilder("MyDataModel");
config.Routes.MapODataRoute("ODataRoute", "odata", modelBuilder.GetEdmModel());
Otherwise I have to do something like this:
private static Microsoft.Data.Edm.IEdmModel BuildODataModel()
ODataModelBuilder modelBuilder = new ODataModelBuilder();
var customers = modelBuilder.EntitySet<Customer>("Customers");
customers.EntityType.HasKey(k => k.CustomerSK);
var orders = modelBuilder.EntitySet<Order>("Orders");
orders.EntityType.HasKey(k => k.OrderSK);
orders.HasRequiredBinding(b => b.Customer, customers);
var lineItems= modelBuilder.EntitySet<LineItem>("LineItem");
lineItems.EntityType.HasKey(k => k.LineItemSK);
lineItems.HasRequiredBinding(b => b.Order, orders);
Any suggestion would be appreciated.
Apr 12, 2013 09:18 PM|raghuramn|LINK
Unfortunately, we don't support models created without ODataModelBuilder. If we did, you could have used the model that EF generated. This is the
issue tracking that on codeplex. Hopefully, we will fix it in this release.
May 01, 2014 05:02 AM|Lybax|LINK
This issue seems to have been fixed now.
However, the solution proposed
here does not work for EF db/model first in my case:
"Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel."
Currently, my only working solution uses ODataConventionModelBuilder and requires me to manually specify all:
This generated model ($metadata) is far to meet all our requirements:
<Property Name="Name" Type="Edm.String" Nullable="false" MaxLength="100" FixedLength="false" Unicode="true" />
<End Type="Client.Objects.Region" Role="Regions" Multiplicity="1" />
<End Type="Client.Objects.Country" Role="Countries" Multiplicity="*" />
<PropertyRef Name="Id" />
<PropertyRef Name="RegionId" />
Is there any chance to have a dynamically generated $metadata in OData Web API someday (as WCF Data Services is not supported anymore)?