如何在Odata模型构建器中加入2个实体 [英] How to join 2 entities in Odata model builder

查看:162
本文介绍了如何在Odata模型构建器中加入2个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用OData V4,希望加入2个表帐户"和产品":

I am currently using OData V4 and wish to join 2 tables Account and Product:

这些表如下: 帐户:ID,名称,地址,颜色代码,

The tables are as follows: Account: Id, Name, Address, ColorCode,

产品:ID,帐户ID

Product: Id, AccountId

产品表中的AccountId是映射到帐户"表中"Id"字段的外键

AccountId in the Product table is a foreign key mapped to the Id field in the Account table

在我的构建器中,我有:

In my builder I have :

var ProductType= builder.EntityType<Product>();

建立产品实体时,我希望从帐户实体中获取"ColorCode"值.

When I build the Product entity I wish to get the "ColorCode" values from the Account entity.

如何在模型构建器中建立这种关系?

How can i establish this relationship in the model builder?

我希望产品类看起来像这样:

I would like the product class to look like this:

public  class Product
{
    public string Id { get; set; }
    public string AccountId { get; set; }
    public string ColorCode { get; set; }

}

推荐答案

OData使您可以定义实体之间的关系.看来您正在使用OData V4的Web API 2.2编写服务.在这种情况下,您可以像下面这样建立产品和帐户之间的关系:

OData enables you to define relationships between entities. It seems that you're using Web API 2.2 for OData V4 to write your service. In this case you can build the relationship between Products and Accounts like this:

首先,在定义产品的POCO类时,您需要为其帐户添加一个导航属性:

Firstly, in the definition of your POCO classes for Products, you need to add a navigation property for its account(s):

public class Product{
    public int Id {get;set;}
    public string AccountId {get;set;}
    public Account Account {get;set;} // define "Account" as a navigation property of Product

public class Account{
    public int Id {get;set;}
    public string Name {get;set;}
    public Address Address {get;set;} // Address may be a complex type
    public int ColorCode {get;set;}
}

然后在继承自DbContext的类中,在以下位置添加有关两个实体的信息:

Then in the class that inherit from the DbContext, add information about both entities in:

public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Account> Accounts { get; set; }

最后,在WebApiConfig.cs中,根据需要使用ODataConventionModelBuilder定义模型.模型构建器将自动识别POCO类中的关系,并且 为您生成模型.

Finally in WebApiConfig.cs, you define the model using ODataConventionModelBuilder according to your need. The model builder will automatically recognize the relationship from the POCO classes and generate the model for you.

在构建服务后,在客户端,客户端发送此请求以获取产品及其帐户的ColorCode:

After the service is built, on the client side, a client send such request to get the Product and the ColorCode of its Account:

GET http://host/service/Products?$expand=Account($select=ColorCode)

可以在此处查看示例: http://服务. odata.org/v4/(S(xrtmlkz1igiqidututicmb2t))/TripPinServiceRW/People ?$ expand = Trips($ select = TripId)

An example can be viewed here: http://services.odata.org/v4/(S(xrtmlkz1igiqidututicmb2t))/TripPinServiceRW/People?$expand=Trips($select=TripId)

这篇关于如何在Odata模型构建器中加入2个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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