如何在 WebAPI Odata V4 中公开数据库视图

How to expose database view in WebAPI Odata V4

我已将视图从数据库导入到 edmx 并将 [Key] 属性添加到模型 POCO class:

命名空间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; }
    }
}

我需要在 webapi odata 控制器中公开此视图,但是当我将以下代码添加到 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 }
            );
        }
    }

但是当我启动应用程序时,我收到错误消息: {"The entity 'vw_Tariffs_PR' does not have a key defined."} 在 变量模型 = builder.GetEdmModel();

在我将 [Key] 属性添加到 TariffId 属性 之后,我收到错误信息:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Код, выполняемый при запуске приложения
        AreaRegistration.RegisterAllAreas();
        **GlobalConfiguration.Configure(WebApiConfig.Register);**
        RouteConfig.RegisterRoutes(RouteTable.Routes);            
    }
}

行GlobalConfiguration.Configure(WebApiConfig.Register); {"ValueFactory try to get acces to property Value of this instance."}

谁能解释一下如何在 odata 控制器中公开视图,我做错了什么?

问题出在WebApiConfig.cs中的路由顺序,odata路由必须在webapi路由之后:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            // Web API routes
            config.MapHttpAttributeRoutes();

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

            // 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);

        }
    }