WebApi OData 4,用于在控制器中批量插入的第二个 POST 端点 [英] WebApi OData 4 , second POST endpoint for bulk insertion in the controller

查看:70
本文介绍了WebApi OData 4,用于在控制器中批量插入的第二个 POST 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 TEAM 模型的基本 WebApi OData 4 控制器,它具有用于将新 TEAM 添加到数据库的 POST 操作.

I have a basic WebApi OData 4 controller for TEAM model which has a POST action for adding a new TEAM to the database.

    public async Task<IHttpActionResult> Post(USER_TEAMS userTeam)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        context.USER_TEAMS.Add(userTeam);
        await context.SaveChangesAsync();
        return Created(userTeam);
    }

我想在该控制器中有另一个端点,用于批量插入团队,它获取团队对象列表并将它们添加到数据库中.WebApi OData 4 中的最佳方法是什么?

I want to have another endpoint in that controller for bulk insertion of teams which takes a list of team objects and adds them to the database. What is the best approach in WebApi OData 4 to do that?

推荐答案

OData 规范包括一个 批处理 概念,但对于这个问题 OData 操作 是一种更清洁的解决方案.该操作将绑定到 Teams 实体集,并将接受请求负载中的 Team 实体集合.(我已在下面的代码中将 USER_TEAMS 更改为 Team.)

The OData spec includes a batching concept, but for this problem an OData action is a cleaner solution. The action will be bound to the Teams entity set and will accept a collection of Team entities in the request payload. (I've changed USER_TEAMS to Team in the code below.)

给定以下Team实体类型的简单定义:

Given the following simple definition of Team entity type:

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
}

首先,在与 Post 方法相同的控制器中定义 action 方法.

First, define the action method in the same controller as your Post method.

    [HttpPost]
    public IHttpActionResult BulkAdd(ODataActionParameters parameters)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest();
        }

        var newTeams = (IEnumerable<Team>)parameters["NewTeams"];

        // Your data access layer logic goes here.

        return this.StatusCode(HttpStatusCode.NoContent);
    }

然后在您的 Web API 配置代码中声明 OData 操作.

Then declare the OData action in your Web API configuration code.

    var builder = new ODataConventionModelBuilder();

    builder.Namespace = "TeamService";
    builder.EntitySet<Team>("Teams");
    builder.EntityType<Team>().Collection
        .Action("BulkAdd")
        .CollectionParameter<Team>("NewTeams");

以上注意事项:

  • EntityTypeConfiguration.Collection 是将操作绑定到 Team 实体集所必需的(相对于单个 Team 实体)
  • ActionConfiguration.CollectionParameter 需要指定参数是一个集合(相对于标量)
  • EntityTypeConfiguration<T>.Collection is necessary to bind the action to the Team entity set (vs. a single Team entity)
  • ActionConfiguration.CollectionParameter<T> is necessary to specify that the parameter is a collection (vs. a scalar)

在客户端上,调用操作如下.

On the client, invoke the action as follows.

POST http://domain/Teams/TeamService.BulkAdd
Content-Type: application/json

{"NewTeams": [{"Name": "Demons"}, {"Name": "Angels"}]}

这篇关于WebApi OData 4,用于在控制器中批量插入的第二个 POST 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆