InvalidOperationException in WebApiConfig File

Jay Vijay Modi 61 Reputation points
2021-07-11T13:02:51.347+00:00

I'm following this video on creating service stack for Entity Framework and the video uses config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel()); in their WebApiConfig file.
I am using the same piece of code in my project and I'm getting this error:

System.InvalidOperationException: 'The complex type 'ClassLibrary1.Session' refers to the entity type 'ClassLibrary1.Customer' through the property 'Customer'.'

for this piece of code:

var builder = new ODataConventionModelBuilder();  
builder.EntitySet<Customer>("Customers");  
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());  

And when I hover over the method it says deprecated:

113540-image.png

So what do I use here to get the same output of the video.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,348 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,146 questions
{count} votes

Accepted answer
  1. Chao Deng-MSFT 796 Reputation points
    2021-07-12T09:42:49.89+00:00

    Hi @Jay Vijay Modi ,

    What version of OData are you using?

    FOR OData V3:

    Install Microsoft.AspNet.WebApi.OData  
    Add using System.Web.Http.OData.Builder; and using System.Web.Http.OData.Extensions;  
    use like config.Routes.MapODataServiceRoute(...)  
    

    FOR OData V4:

        Install Microsoft.AspNet.OData  
        Add using System.Web.OData.Builder; and using System.Web.OData.Extensions;  
        use like config.MapODataServiceRoute(...)  
    

    You can refer to this article:https://stackoverflow.com/questions/27224702/config-mapodataserviceroute-error


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng

    0 comments No comments

0 additional answers

Sort by: Most helpful