简体   繁体   English

OData Expand覆盖实体框架包括

[英]OData Expand override Entity Framework includes

Consider the following domain model: 请考虑以下域模型:

public class Employee 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AddressId { get; set; }
    public Address Address { get; set; }
    public ICollection<Message> Messages { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    .....
}

public class Message
{
    public int Id { get; set; }
    public Message Message { get; set; }
}

We use EF6 code first to create the database for this model. 我们首先使用EF6代码为该模型创建数据库。 The address data is saved to its own table. 地址数据将保存到其自己的表中。 On top of EF6 we use ASP.NET Web API 2.0 and OData. 在EF6之上,我们使用ASP.NET Web API 2.0和OData。 The ODataConventionModelBuilder creates an EntitySet for Employee and a ComplexType for Address. ODataConventionModelBuilder为Employee创建一个EntitySet,为Address创建一个ComplexType。

In order to return an Employee we created the following action: 为了返回雇员,我们创建了以下操作:

[EnableQuery]
public SingleResult<Employee> Get([FromODataUri] int key)

In order to always return the Address we use an EF Include on the Address property. 为了始终返回地址,我们在Address属性上使用EF Include。 So when we use the following url: 因此,当我们使用以下网址时:

/api/Employees(1)

The Employee with id 1 will be returned with its Address property filled. ID为1的Employee将被填充其Address属性。

However, when we use $expand to expand the Messages for the use we loose the Address data: 但是,当我们使用$expand扩展Messages以供使用时,我们将松开Address数据:

/api/Employee(1)?$expand=Messages

Returns an Employee object with Messages expanded, but without the Address data. 返回扩展了“消息”但不包含“地址”数据的Employee对象。 The EnableQuery attribute applies the expand to our query-able but overwrites the EF Includes that we specified in the API. EnableQuery属性将扩展应用于我们的可查询属性,但会覆盖我们在API中指定的EF Includes。 We cannot expand the Address too from the client because Complex types cannot be expanded in OData. 我们也不能从客户端扩展地址,因为不能在OData中扩展复杂类型。 When we change the Address Odata type to an Entity we need multiple calls to create or update an Employee. 当我们将地址数据类型更改为实体时,我们需要多个调用来创建或更新Employee。 One call to an Address controler which handles create/update for addresses and one call to the Employee controller to handle create/update for employees. 一次调用地址控制器处理地址的创建/更新,一次调用Employee控制器处理雇员的创建/更新。

So my question : Is it possible to preserve the Address include without specifying it from the client? 所以我的问题是:是否可以在不从客户端指定的情况下保留Address include?

We use element type like Employee when select all properties, so we lose your include Address when composing the Linq expression, but if you select Address, it will come in result because it's not select all, so a work around maybe request like: $expand=Messages&$select=Address,Id,Name 在选择所有属性时,我们使用诸如Employee之类的元素类型,因此在编写Linq表达式时会丢失您的include Address,但是如果选择Address,它将因为没有选择全部而出现在结果中,因此可能需要以下解决方法:$ expand = Messages&$ select =地址,ID,名称

Relative code: https://github.com/OData/WebApi/blob/master/OData/src/System.Web.OData/OData/Query/Expressions/SelectExpandBinder.cs#L272-L292 相对代码: https : //github.com/OData/WebApi/blob/master/OData/src/System.Web.OData/OData/Query/Expressions/SelectExpandBinder.cs#L272-L292

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

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