简体   繁体   English

使用MVC Web Api进行ODATA和CRUD操作

[英]ODATA and CRUD operations with MVC Web Api

I need to create a service (currently with ASP.NET Web Api) that is taking data from SQL database and exposes them using OData protocol. 我需要创建一个服务(当前使用ASP.NET Web Api),该服务从SQL数据库获取数据并使用OData协议公开它们。 I'm extremely new to OData and Web Api. 我是OData和Web Api的新手。

The problem is I don't know how to perform crud operations - I've looked through some samples available online but my case is a little bit more complicated (or maybe not, hard to say). 问题是我不知道如何执行原始操作-我已经浏览了一些在线示例,但我的情况有些复杂(或者也许很难说)。 I'm using Entity Framework. 我正在使用实体框架。

Sometimes I need to do the "GET" with querying table directly but do "POST" with stored procedure. 有时我需要直接使用查询表执行“ GET”操作,但是使用存储过程执行“ POST”操作。 The biggest problem i have is limitation in parameters amount. 我最大的问题是参数数量的限制。 As I understand I can have only one. 据我了解,我只能拥有一个。

This is rather an issue in my case: 就我而言,这是一个问题:

public class ProjectsController : ODataController
{
    [EnableQuery]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public IQueryable<GetProjects_Result> Get(string key)
    {
        var context = new OPMLVSQL001Entities();
        IQueryable<GetProjects_Result> results = context.GetProjects(key);
        return results;
    }

    [EnableQuery]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public GetProject_Result Get(int projectId, string loginName)
    {
        var context = new OPMLVSQL001Entities();
        GetProject_Result result = context.GetProject(projectId, loginName).FirstOrDefault();
        return result;
    }

that code won't work - just to get all projects, I need to pass a login parameter. 该代码将无法正常工作-仅为了获取所有项目,我需要传递一个登录参数。 To get one entity, I need to use different method - and I have to pass two parameters. 要获得一个实体,我需要使用不同的方法-我必须传递两个参数。 Which I don't know how to do in OData (I thought of a hack - passing json table in the only parameter I can use). 我不知道该如何在OData中做(我想到了一个hack-在我可以使用的唯一参数中传递json表)。

So is there a way to use more than one parameter? 那么有没有办法使用多个参数呢?

I also have to implement a "create" method - an entity is being created with stored procedure again. 我还必须实现“创建”方法-再次使用存储过程创建实体。 What is the best way to follow in this case? 在这种情况下最好的遵循方法是什么?

One way probably is to add, Project entity to the project and do post like this: 一种方法可能是将Project实体添加到项目中,并像这样进行发布:

    [EnableQuery]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public HttpResponseMessage Post(Project project)
    {
    }

but what if I have some logic in dbo.CreateProject([bunch of parameters]) that I need to run along and for that reason I'd like to run the stored procedure. 但是如果我需要在dbo.CreateProject([bunch of parameters])中运行一些逻辑,并且由于这个原因,我想运行存储过程,该怎么dbo.CreateProject([bunch of parameters]) Use values from projects to initialize dbo.CreateProjects parameters? 使用项目中的值初始化dbo.CreateProjects参数? I really don't know what's the best way to follow. 我真的不知道什么是最好的跟随方式。

What exactly do you mean saying that you want to pass more than one parameter? 您到底想传递多个参数,这是什么意思? OData definitely support complex queries with multiple filter parameters and much more . OData绝对支持具有多个过滤器参数以及更多其他条件的复杂查询。

While making queries, OData allows you to define how exactly data should be retrieved, filtered and projected. 在进行查询时, OData允许您定义应如何精确地检索,过滤和投影数据。 You can use any number of parameters when filtering. 过滤时可以使用任意数量的参数。 For example, the following query string: 例如,以下查询字符串:

/people?$skip=10&$top=5&$select=FirstName, LastName&$filter=Address eq 'Redmond'

will be translated to something like 将被翻译成类似

queryable
    .Where(p => p.Address == "Redmond")
    .Skip(10)
    .Take(5)
    .Select(p => new { p.FirstName, p.LastName });

Assuming that you are using Entity Framework, this query will be further translated into SQL database query. 假设您使用的是Entity Framework,则此查询将进一步转换为SQL数据库查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM