I have a class called Application

public class Application
{
    public virtual Guid Id { get; internal set; }
    public virtual string Code { get; internal set; }
    public virtual string Name { get; internal set; }
    public virtual DateTime DateCreated { get; internal set; }
}

I map the Code First model configuration using Fluent API

public class ApplicationConfiguration : EntityTypeConfiguration<Application>
{
    public ApplicationConfiguration()
    {
        HasKey(p => p.Id);
        Property(p => p.Code).HasMaxLength(50).IsRequired();
        Property(p => p.Name).HasMaxLength(256).IsRequired();
        Property(p => p.DateCreated).HasColumnType("date");
    }
}

public class ApplicationsUnitOfWork : DbContext
{
    public DbSet<Application> Applications { get; set; }  

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Configurations.Add(new ModelsConfiguration.ApplicationConfiguration());
    }
}

Now, i need to implement OData for the Application entities.

Here is the configuration:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        ODataModelBuilder builder = new ODataConventionModelBuilder();
        var applicationConfig = builder.EntitySet<Application>("Applications");
        applicationConfig.EntityType.HasKey(p => p.Id);
        applicationConfig.EntityType.Property(p => p.Id).Name = "id";
        applicationConfig.EntityType.Property(p => p.Name).Name = "name";
        applicationConfig.EntityType.Ignore(p => p.DateCreated);

        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "odata",
            model: builder.GetEdmModel());

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

As you can see, the specified OData configuration works only under "odata" route. Ex: localhost:port\odata\Applications.

Is there a way to apply the OData configuration globally?

Related posts

Recent Viewed