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

oData Web.API中两个EdmEntityType之间的关系,api,asp.net-web-api,odata,Api,Asp.net Web Api,Odata,我正在使用web.api oData,并使用dynamic with EdmEntityType对我的数据模型进行优化。我可以使用Excel Power Query读取oData流,但必须添加EdmEntityTypes/表之间的关系。 我现在正在使用EdmNavigationPropertyInfo进行测试,但无法以这种方式在模型中设置信息,Power Query可以读取关系。 简单的例子: EdmEntityType产品和产品组 类型产品 产品ID 产品名称 FK_ProductGroupI

我正在使用web.api oData,并使用dynamic with EdmEntityType对我的数据模型进行优化。我可以使用Excel Power Query读取oData流,但必须添加EdmEntityTypes/表之间的关系。 我现在正在使用EdmNavigationPropertyInfo进行测试,但无法以这种方式在模型中设置信息,Power Query可以读取关系。 简单的例子: EdmEntityType产品和产品组 类型产品 产品ID 产品名称 FK_ProductGroupID

类型产品组 PK_ProductGroupID ProductGroupName

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

致意
Christoph

如果您希望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/$元数据,可以得到元数据文档odatav3格式,如下:

<?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>
元数据文档中提供了引用约束

希望它能帮助你。谢谢