I have imported view from database to edmx and added [Key] attribute to model POCO class:
namespace TFOMS.Domain.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class vw_Tariffs_PR
{
public int TariffId { get; set; }
public string MCOD { get; set; }
public string OrgName { get; set; }
public Nullable<int> IDPR { get; set; }
public string PRNAME { get; set; }
public Nullable<int> IDSP { get; set; }
public string SPNAME { get; set; }
public Nullable<byte> isChild { get; set; }
public decimal tariff { get; set; }
public System.DateTime DATEBEG { get; set; }
public Nullable<System.DateTime> DATEEND { get; set; }
}
}
I need to expose this view in webapi odata controller, but when I add following code to WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<vw_Tariffs_PR>("vw_Tariffs_PR");
var model = builder.GetEdmModel();
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: model);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
But when I start application I receive error: {"The entity 'vw_Tariffs_PR' does not have a key defined."} in var model = builder.GetEdmModel();
After I add [Key] attribute to TariffId property I receive error in:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Код, выполняемый при запуске приложения
AreaRegistration.RegisterAllAreas();
**GlobalConfiguration.Configure(WebApiConfig.Register);**
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
In line GlobalConfiguration.Configure(WebApiConfig.Register); {"ValueFactory try to get acces to property Value of this instance."}
Can anybody explain how to expose view in odata controller, what I do wrong?