1

I'm building a OData service using the ODataLib v7.1.1 bits from Microsoft.OData.Core (et al). I'm having trouble using the shortened key predicate URL format with ODataUriParser.ParsePath(). The (relative) URL "Company('Comp1')/Customer(1)" throws an exception "The number of keys specified in the URI does not match number of key properties for the resource 'Test.Customer'".

The model is very simple and I can't see why this isn't working. Other formats for the URLs work ("Company('Comp1')", "Customer(CompanyId='COMP1',CustNum=1)").

I've put the model builder code below along with the $metadata output. Any help would be appreciated. Thanks!

-Erik

    var result = new EdmModel();
        var company = new EdmEntityType("Test", "Company");
        var companyKey1 = company.AddStructuralProperty("CompanyId", EdmPrimitiveTypeKind.String, false);
        company.AddKeys(
            companyKey1
            );
        result.AddElement(company);


        var customer = new EdmEntityType(NS, "Customer");
        var customerKey1 = customer.AddStructuralProperty("CompanyId", EdmPrimitiveTypeKind.String, false);
        var customerKey2 = customer.AddStructuralProperty("CustNum", EdmPrimitiveTypeKind.Int32, false);

        customer.AddKeys(
            customerKey1,
            customerKey2
            );
        result.AddElement(customer);

        var navCompanyCustomer = company.AddUnidirectionalNavigation(
            new EdmNavigationPropertyInfo()
            {
                ContainsTarget = true,
                Name = "Customer",
                Target = customer,
                TargetMultiplicity = EdmMultiplicity.Many
            });

        var customerRefToCompany = customer.AddUnidirectionalNavigation(
            new EdmNavigationPropertyInfo()
            {
                ContainsTarget = false,
                Name = "Company",
                Target = company,
                TargetMultiplicity = EdmMultiplicity.One,
                DependentProperties = new[] { customerKey1 },
                PrincipalProperties = new[] { companyKey1 }
            });

        var container = new EdmEntityContainer("Test", "DefaultContainer");
        result.AddElement(container);
        container.AddEntitySet("Company", company);
        container.AddEntitySet("Customer", customer);

        return result;

<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
  <edmx:DataServices>
    <Schema Namespace="Test" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityType Name="Company">
        <Key>
          <PropertyRef Name="CompanyId" />
        </Key>
        <Property Name="CompanyId" Type="Edm.String" Nullable="false" />
        <NavigationProperty Name="Customer" Type="Collection(Test.Customer)" ContainsTarget="true" />
      </EntityType>
      <EntityType Name="Customer">
        <Key>
          <PropertyRef Name="CompanyId" />
          <PropertyRef Name="CustNum" />
        </Key>
        <Property Name="CompanyId" Type="Edm.String" Nullable="false" />
        <Property Name="CustNum" Type="Edm.Int32" Nullable="false" />
        <NavigationProperty Name="Company" Type="Test.Company" Nullable="false">
          <ReferentialConstraint Property="CompanyId" ReferencedProperty="CompanyId" />
        </NavigationProperty>
      </EntityType>
      <EntityContainer Name="DefaultContainer">
        <EntitySet Name="Company" EntityType="Test.Company" />
        <EntitySet Name="Customer" EntityType="Test.Customer" />
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

1 Answers1

1

I figured it out. I had the Dependent / Principal properties reversed (corrected code is below). One of the big debates (years ago) was which way the arrow should point on an RI declaration in entity-relationship model (i.e. which side is dependent). I must of been on the losing side of that one. ;)

 var navCompanyCustomer = company.AddUnidirectionalNavigation(
     new EdmNavigationPropertyInfo()
     {
         ContainsTarget = true,
         Name = "Customer",
         Target = customer,
         TargetMultiplicity = EdmMultiplicity.Many,
         DependentProperties = new[] { companyKey1 },
         PrincipalProperties = new[] { customerKey1 }
     });