简体   繁体   English

使用OData和Entity Framework返回实体列表

[英]Return a list of entities with OData & Entity Framework

I have an OData method and I want to return a List of entities from that method. 我有一个OData方法,我想从该方法返回一个实体列表。

This is my code 这是我的代码

public async Task<IHttpActionResult> LoadSpecimenMachinings([FromODataUri] Int32 key)
{
    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    Int32 stage = 1;
    string csvSpecimenCodes = "S0,S1,S2";

    List<Machining> machinings = null;
    var machiningResult = db.LoadSpecimenMachinings(key, stage, csvSpecimenCodes);
    machinings = (from machining in machiningResult select machining).ToList();

    return Created(machinings);
}

When I returned a List of entities (Machining is an entity in my Entity Framework Model) in the line return Created(machinings); 当我在返回的行中返回实体列表(机械加工是我的实体框架模型中的实体)时, return Created(machinings); I received the following error: 我收到以下错误:

"message":" is not an entity type. Only entity types are supported.",
"type":"System.InvalidOperationException"

As I understand, unfortunately Created cannot receive a List of Entities as parameter. 据我了解,不幸的是Created不能接收实体列表作为参数。 Is there any way to return a list of entities and a Created HTTP Code in OData ? 有什么方法可以在OData中返回实体列表和创建的HTTP代码吗?

I am using OData V3 and Entity Framework 6. 我正在使用OData V3和Entity Framework 6。

return Created() is designed to only return a single element and for use when you do an insert, this returns a 201 Created status code return Created()设计为仅返回单个元素,并在您执行插入操作时使用,这将返回201 Created状态代码

You should use return Ok(machinings) this returns a 200 OK status code 您应该使用return Ok(machinings)这将返回200 OK状态代码

See the OData specification on response code http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part1-protocol/odata-v4.0-errata02-os-part1-protocol-complete.html#_Toc406398250 请参阅有关响应代码的OData规范http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part1-protocol/odata-v4.0-errata02-os-part1-protocol -complete.html#_Toc406398250

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

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