简体   繁体   English

在实体框架中通过 id 查询相关实体

[英]Query a related entity by id in Entity Framework

I use the c# web api in combination with the entity framework.我将 c# web api 与实体框架结合使用。 The http requests are generated by the sap ui5 framework. http 请求由 sap ui5 框架生成。 The generated get request looks like: "http://localhost:52854/KddietzTourenplan/5/KddietzTour/1" How must the name of the associated method and parameters?生成的获取请求看起来像:“http://localhost:52854/KddietzTourenplan/5/KddietzTour/1” 关联方法和参数的名称如何?

    [EnableQuery]
    public IQueryable<KddietzTour> GetKddietzTour([FromODataUri] int key, ???)
    {
        return _oContext.KddietzTour.AsQueryable().Where(p => p.NTourenplanId == key);
    }

I know, the simplest request would be "http://localhost:52854/KddietzTour/1".我知道,最简单的请求是“http://localhost:52854/KddietzTour/1”。 But as mentioned before, the requests are generated from another framework.但如前所述,请求是从另一个框架生成的。

You need to use HttpGet Attribute to map the URL to the method and its parameters.您需要使用 HttpGet 属性将 URL 映射到方法及其参数。 to map the parameters you need to use {ParameterName} inside the template you're using in HttpGet映射您需要在 HttpGet 中使用的模板内使用{ParameterName}{ParameterName}

[EnableQuery]
[HttpGet("KddietzTourenplan/5/KddietzTour/{key}")]
public IQueryable<KddietzTour> GetKddietzTour([FromODataUri] int key, ???)
{
    return _oContext.KddietzTour.AsQueryable().Where(p => p.NTourenplanId == key);
}

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

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