簡體   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