如何从数据库中为apicontroller公开存储过程

时间:2017-11-09 02:42:38

标签: c# asp.net-web-api entity-framework-6 asp.net-web-api2 visual-studio-2017

我很难理解使用EF公开存储过程的推荐方法,以及这样做是否有好处。

我实现了一个数据库优先代码来包装存储过程,该过程在上下文中生成如下内容:

public virtual ObjectResult<PWB_P_GetAvailableOfferings_Result> PWB_P_GetAvailableOfferings(Nullable<int> productID, Nullable<System.DateTime> purchaseDate, Nullable<int> accountID, Nullable<int> locationID)
{
        var productIDParameter = productID.HasValue ?
            new ObjectParameter("ProductID", productID) :
            new ObjectParameter("ProductID", typeof(int));

        var purchaseDateParameter = purchaseDate.HasValue ?
            new ObjectParameter("PurchaseDate", purchaseDate) :
            new ObjectParameter("PurchaseDate", typeof(System.DateTime));

        var accountIDParameter = accountID.HasValue ?
            new ObjectParameter("AccountID", accountID) :
            new ObjectParameter("AccountID", typeof(int));

        var locationIDParameter = locationID.HasValue ?
            new ObjectParameter("LocationID", locationID) :
            new ObjectParameter("LocationID", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PWB_P_GetAvailableOfferings_Result>("PWB_P_GetAvailableOfferings", productIDParameter, purchaseDateParameter, accountIDParameter, locationIDParameter);
    }

然后我为它创建了一个控制器:

public class AvailableOfferingsController : ApiController
{
    private readonly ProductConfigurationContext _repository =
        new ProductConfigurationContext();

    [HttpGet]
    public ObjectResult<PwbPGetAvailableOfferingsResult> Get( int? productId, int? segmentId, int? channelId, int? salesRegionId, bool? returnExpiredOfferings, int? accountId)
    {
        return _repository.PWB_P_GetProductOfferings(productId, segmentId, channelId, salesRegionId, returnExpiredOfferings, accountId);
    }
}

我的启动代码:

public static class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public static void ConfigureApp(IAppBuilder appBuilder)
    {
        appBuilder.Use(typeof(NotFoundMiddleware));

        // Configure Web API for self-host. 
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        appBuilder.UseWebApi(config);
    }
}

我的控制器没有响应。我发送了针对https://myservice/AvailableOfferings/的GET请求并获得了超时。

我做错了什么?

0 个答案:

没有答案