繁体   English   中英

oData Web.API中两个EdmEntityType之间的关系

[英]Relations between two EdmEntityType in oData Web.API

我正在使用web.api oData,并通过EdmEntityType动态优化了我的数据模型。 我可以使用Excel Power Query读取oData-Stream,但必须添加EdmEntityTypes / Tables之间的关系。 我现在正在使用EdmNavigationPropertyInfo进行测试,但是无法以这种方式在模型中设置信息,因此Power Query可以读取关系。 简单示例:EdmEntityType产品和ProductGroup 类型产品 ProductID ProductName FK_ProductGroupID

类型 ProductGroup PK_ProductGroupID产品组名称

我可以阅读这两种类型,并手动将Product / FK_ProductGroupID与ProductGroup / PK_ProductGroupID连接。 我可以直接在模型中创建此关系吗? 如何定义FK-Field和PK-Field的连接?

最好的问候克里斯托夫

如果您希望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>

参考约束在元数据文档中提供。

希望它能对您有所帮助。 谢谢。

暂无
暂无

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

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