我正在使用web.api oData并使用EdmEntityType动态化我的数据模型。我可以使用Excel Power Query读取oData-Stream,但必须添加EdmEntityTypes / Tables之间的关系。 我现在正在使用EdmNavigationPropertyInfo进行测试,但无法以这种方式在模型中设置信息,Power Query可以读取关系。 简单的例子: EdmEntityType产品和产品组 输入产品 产品ID 产品名称 FK_ProductGroupID
输入ProductGroup PK_ProductGroupID ProductGroupName
我可以阅读这两种类型,并手动将Product / FK_ProductGroupID与ProductGroup / PK_ProductGroupID连接。 我可以直接在模型中创建这种关系吗?如何定义FK-Field和PK-Field的连接?
祝你好运 克里斯托弗
答案 0 :(得分:0)
如果你想要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 },
});
添加路线(V3):
Configuration.Routes.MapODataServiceRoute("odata", "odata", model);
查询~/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>
参考约束在元数据文档中显示。
希望它可以帮到你。感谢。