OData InvalidOperationException。错误的请求-查询语法错误

时间:2019-03-26 16:31:25

标签: asp.net-core-mvc asp.net-core-webapi odata-v4

使用ASP.NET Core和OData v4

我得到一个

  

InvalidOperationException:路径模板   动作“ GetBooking”中的“类({key})/ Bookings({bookingKey})”   控制器“类”不是有效的OData路径模板。错误的请求 -   查询语法错误。

我没有在查询语法中看到错误。这是我的控制器“类”中此方法的完整代码

    /// <summary>
    /// Get a specific booking
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ODataRoute("Classes({key})/Bookings({bookingKey})")]
    public async Task<IActionResult> GetBooking([FromODataUri] Guid key, [FromODataUri] Guid bookingKey)
    {
        var @class = await _context.Classes.FirstOrDefaultAsync(y => y.Id == key);

        if (@class == null)
        {
            return NotFound();
        }

        var booking = _context.Bookings.Where(y => y.Class.Id == key && y.Id == bookingKey);

        if (!booking.Any())
        {
            return NotFound();
        }

        return Ok(SingleResult.Create(booking));
    }

此方法在“类”控制器中定义。我还定义了一个GeBookings方法和2个动作方法,这些都不会引起问题。当我评论我的GetBooking()方法时,我没有任何错误。

预订是[包含]预订的ICollection

public class Class
{
    [Key]
    public Guid Id { get; set; }

    // Others properties

    [Contained]
    public ICollection<Booking> Bookings {get; set;}
}

我真的看到了我的错误。我应该看吗?

编辑更多信息

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
    <edmx:DataServices>
        <Schema Namespace="Oyg" xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <EntityType Name="Owner">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <Property Name="Email" Type="Edm.String" />
                <Property Name="PhoneNumber" Type="Edm.String" />
                <Property Name="Hash" Type="Edm.String" />
                <Property Name="ConfigurationString" Type="Edm.String" />
                <NavigationProperty Name="Teachers" Type="Collection(Oyg.Teacher)" ContainsTarget="true" />
                <NavigationProperty Name="Locations" Type="Collection(Oyg.Location)" ContainsTarget="true" />
                <NavigationProperty Name="Classes" Type="Collection(Oyg.Class)" ContainsTarget="true" />
            </EntityType>
            <EntityType Name="Student">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <Property Name="Email" Type="Edm.String" />
                <Property Name="PhoneNumber" Type="Edm.String" />
                <Property Name="Hash" Type="Edm.String" />
                <Property Name="ConfigurationString" Type="Edm.String" />
            </EntityType>
            <EntityType Name="Class">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Title" Type="Edm.String" />
                <Property Name="Description" Type="Edm.String" />
                <Property Name="StartTime" Type="Edm.DateTimeOffset" Nullable="false" />
                <Property Name="EndTime" Type="Edm.DateTimeOffset" Nullable="false" />
                <Property Name="Status" Type="Oyg.ScheduleStatus" Nullable="false" />
                <Property Name="Positions" Type="Edm.Int32" Nullable="false" />
                <Property Name="Bookings" Type="Collection(Oyg.Booking)" />
                <NavigationProperty Name="Owner" Type="Oyg.Owner" />
                <NavigationProperty Name="Teacher" Type="Oyg.Teacher" />
                <NavigationProperty Name="Location" Type="Oyg.Location" />
            </EntityType>
            <ComplexType Name="Booking">
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Position" Type="Edm.Int32" Nullable="false" />
                <Property Name="ReservationTime" Type="Edm.DateTimeOffset" Nullable="false" />
                <Property Name="CancelTime" Type="Edm.DateTimeOffset" />
                <NavigationProperty Name="Class" Type="Oyg.Class" Nullable="false" />
                <NavigationProperty Name="Student" Type="Oyg.Student" Nullable="false" />
            </ComplexType>
            <EntityType Name="Teacher">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <Property Name="Description" Type="Edm.String" />
                <Property Name="Email" Type="Edm.String" />
                <Property Name="PhoneNumber" Type="Edm.String" />
                <Property Name="Website" Type="Edm.String" />
                <NavigationProperty Name="Owner" Type="Oyg.Owner" />
            </EntityType>
            <EntityType Name="Location">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.Guid" Nullable="false" />
                <Property Name="Title" Type="Edm.String" />
                <Property Name="Description" Type="Edm.String" />
                <Property Name="Geolocalisation" Type="Edm.String" />
                <Property Name="Address" Type="Edm.String" />
                <Property Name="Facilities" Type="Edm.String" />
                <NavigationProperty Name="Owner" Type="Oyg.Owner" />
            </EntityType>
            <EnumType Name="ScheduleStatus">
                <Member Name="Draft" Value="0" />
                <Member Name="Published" Value="1" />
                <Member Name="Cancelled" Value="2" />
            </EnumType>
            <EntityContainer Name="OygContainer">
                <EntitySet Name="Owners" EntityType="Oyg.Owner" />
                <EntitySet Name="Students" EntityType="Oyg.Student" />
                <EntitySet Name="Classes" EntityType="Oyg.Class">
                    <NavigationPropertyBinding Path="Bookings/Class" Target="Classes" />
                    <NavigationPropertyBinding Path="Bookings/Student" Target="Students" />
                    <NavigationPropertyBinding Path="Owner" Target="Owners" />
                </EntitySet>
            </EntityContainer>
        </Schema>
        <Schema Namespace="Oyg.Actions" xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <Action Name="Register" IsBound="true">
                <Parameter Name="bindingParameter" Type="Oyg.Class" />
                <Parameter Name="studentId" Type="Edm.Guid" Nullable="false" />
                <ReturnType Type="Oyg.Booking" />
            </Action>
            <Action Name="CancelRegistration" IsBound="true">
                <Parameter Name="bindingParameter" Type="Oyg.Class" />
                <Parameter Name="studentId" Type="Edm.Guid" Nullable="false" />
            </Action>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

似乎我的ICollection of Booking并没有真正像集合一样被视为包含的导航属性

{
    "@odata.context": "https://localhost:44375/odata/$metadata#Classes",
    "value": [
        {
            "Id": "dde8de4d-89ca-4ba3-cff7-08d6aac63c0f",
            "Title": "xxx",
            "Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
            "StartTime": "2019-02-24T18:00:00+08:00",
            "EndTime": "2019-02-24T19:00:00+08:00",
            "Status": "Draft",
            "Positions": 22,
            "Bookings": []
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

来自xuzhg http://lwp.interglacial.com/appc_01.htm

的答案
  

Bookings配置为“ Structural property”,其类型为   复杂类型。复杂类型是没有密钥的结构类型,因此,   您的路径模板“ Classes({id})/ Bookings({bookingId})”无法正常工作。

     

根本原因是调用“ registerAction.Returns()”,此行将   将“预订”类型配置为复杂类型。我想如果你打电话   “ registerAction.ReturnsFromEntitySet(“ Bookings”)”可以解析   问题。谢谢