Must register all my domain entities in ODataConventionModelBuilder?

Topics: ASP.NET Web API
Jul 5, 2013 at 12:34 PM
Edited Jul 5, 2013 at 12:43 PM
I have several domain classes all inherit from IEntity

Why can not I do modelBuilder.EntitySet<IEntity>(" entities "); and this register all classes that inherit from IEntity?

WebApiConfig.cs

public static ODataConventionModelBuilder Register(HttpConfiguration config)
{
    var modelBuilder = new ODataConventionModelBuilder(config);
    modelBuilder.EntitySet<IEntity>("entities");
    modelBuilder.Namespace = "CreditoImobiliarioBB.Domain";
    var model = modelBuilder.GetEdmModel();
            
    config.Routes.MapODataRoute(routeName: "OData", routePrefix: "odata", model: model);
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    config.EnableQuerySupport();

    var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    var enumConverter = new StringEnumConverter();
    jsonFormatter.SerializerSettings.Converters.Add(enumConverter);

    config.Formatters.Remove(config.Formatters.XmlFormatter);
    var jqueryFormatter = config.Formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter));
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
    config.Formatters.Remove(jqueryFormatter);
    config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;

    return modelBuilder;
}

Attempts

All return 404

GET /api/Modalidades or /odata/Modalidades

in modelBuilder.EntitySets I have 1 entity set only.
Jul 5, 2013 at 6:59 PM
We don't register entitysets automatically for a given entity type for a couple of reasons,

1) We support MEST (multiple entity sets of same type). So, it complicates our model builder APIs if we start adding entity sets automatically.
2) It is a lot of code and complexity to pluralize entity set names. Also, the entity set name is part of the URL. So, people would like to customize it or have custom conventions for figuring it out.

And it is not a lot of code to tell the model builder all the entity sets - one line for each entity set. And once we have this bug fixed (802: Support external creation of IEdmModel ), you can directly expose your Entity framework model as well without even using the modelbuilder.
Jul 5, 2013 at 7:58 PM
raghuramn wrote:
We don't register entitysets automatically for a given entity type for a couple of reasons,

1) We support MEST (multiple entity sets of same type). So, it complicates our model builder APIs if we start adding entity sets automatically.
2) It is a lot of code and complexity to pluralize entity set names. Also, the entity set name is part of the URL. So, people would like to customize it or have custom conventions for figuring it out.

And it is not a lot of code to tell the model builder all the entity sets - one line for each entity set. And once we have this bug fixed (802: Support external creation of IEdmModel ), you can directly expose your Entity framework model as well without even using the modelbuilder.
" you can directly expose your Entity framework model as well without even using the modelbuilder."
How can I do this? Could you provide some example code?
Jul 5, 2013 at 8:35 PM
I haven't checked in the fix for that bug yet. It is in code review right now. I will share a sample as soon as (very soon) that change is pushed.