[英]Relations between two EdmEntityType in oData Web.API
I'm using web.api oData and greate my data model with dynamic with EdmEntityType. 我正在使用web.api oData,并通过EdmEntityType动态优化了我的数据模型。 I can read the oData-Stream with Excel Power Query, but have to add the relations between the EdmEntityTypes/Tables.
我可以使用Excel Power Query读取oData-Stream,但必须添加EdmEntityTypes / Tables之间的关系。 I'm now testing with EdmNavigationPropertyInfo, but can't set the info in model in that way, that Power Query could read the relations.
我现在正在使用EdmNavigationPropertyInfo进行测试,但是无法以这种方式在模型中设置信息,因此Power Query可以读取关系。 Simple example: EdmEntityType Product and ProductGroup Type Product ProductID ProductName FK_ProductGroupID
简单示例:EdmEntityType产品和ProductGroup 类型产品 ProductID ProductName FK_ProductGroupID
Type ProductGroup PK_ProductGroupID ProductGroupName 类型 ProductGroup PK_ProductGroupID产品组名称
I can read this two types and connect Product/FK_ProductGroupID with ProductGroup/PK_ProductGroupID by hand. 我可以阅读这两种类型,并手动将Product / FK_ProductGroupID与ProductGroup / PK_ProductGroupID连接。 Can I create this relationship directly in model?
我可以直接在模型中创建此关系吗? How can I define that die FK-Field und PK-Field are connected?
如何定义FK-Field和PK-Field的连接?
Best regards Christoph 最好的问候克里斯托夫
if you want the referential constraint (foreign key) supported in web api, I create a sample that you can refer to: 如果您希望Web api支持引用约束(外键),我创建一个示例,您可以参考:
EdmModel model = new EdmModel();
EdmEntityType customer = new EdmEntityType("DefaultNamespace", "Customer");
customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmCoreModel.Instance.GetInt32(false)));
customer.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
model.AddElement(customer);
EdmEntityType order = new EdmEntityType("DefaultNamespace", "Order");
order.AddKeys(order.AddStructuralProperty("OrderId", EdmCoreModel.Instance.GetInt32(false)));
EdmStructuralProperty orderCustomerId = order.AddStructuralProperty("CustomerForeignKey", EdmCoreModel.Instance.GetInt32(true));
model.AddElement(order);
customer.AddBidirectionalNavigation(
new EdmNavigationPropertyInfo
{
Name = "Orders",
Target = order,
TargetMultiplicity = EdmMultiplicity.Many
},
new EdmNavigationPropertyInfo
{
Name = "Customer",
TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
DependentProperties = new[] { orderCustomerId },
});
Add the routes (V3): 添加路线(V3):
Configuration.Routes.MapODataServiceRoute("odata", "odata", model);
Query the ~/odata/$metadata
, you can get the metadata document (OData V3 format) as below: 查询
~/odata/$metadata
,您可以获取元数据文档(OData V3格式),如下所示:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="DefaultNamespace" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="Customer">
<Key>
<PropertyRef Name="CustomerId" />
</Key>
<Property Name="CustomerId" Type="Edm.Int32" Nullable="false" />
<Property Name="Name" Type="Edm.String" Nullable="false" />
<NavigationProperty Name="Orders" Relationship="DefaultNamespace.DefaultNamespace_Order_Customer_DefaultNamespace_Customer_Orders" ToRole="Orders" FromRole="Customer" />
</EntityType>
<EntityType Name="Order">
<Key>
<PropertyRef Name="OrderId" />
</Key>
<Property Name="OrderId" Type="Edm.Int32" Nullable="false" />
<Property Name="CustomerForeignKey" Type="Edm.Int32" />
<NavigationProperty Name="Customer" Relationship="DefaultNamespace.DefaultNamespace_Order_Customer_DefaultNamespace_Customer_Orders" ToRole="Customer" FromRole="Orders" />
</EntityType>
<Association Name="DefaultNamespace_Order_Customer_DefaultNamespace_Customer_Orders">
<End Type="DefaultNamespace.Customer" Role="Customer" Multiplicity="0..1" />
<End Type="DefaultNamespace.Order" Role="Orders" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Customer">
<PropertyRef Name="CustomerId" />
</Principal>
<Dependent Role="Orders">
<PropertyRef Name="CustomerForeignKey" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
The referential constraint is presented in metadata document. 参考约束在元数据文档中提供。
Hope it can help you. 希望它能对您有所帮助。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.