Hello,
I am implementing an OData v.4.0 web service using the latest version 7.6.4 and I use the Non-Convention model builder to create my EDM model.
I have a base class Person with a Derived class Customer. The Customer class is related to the Order class in a one to many relationship where a customer can have many orders and an order has one customer.
Person
Customer : Person
- Orders : Collection<Order>
Order
- Id : Guid (Key)
- Customer : Customer
- CustomerId : Guid (Foreign Key)
I create an entity set and entity type for all the types above and I specified the inheritance like this:
customerTypeConfig.DerivesFrom(personTypeConfig);
Key properties are defined in Person and in Order:
entityTypeConfig.HasKey(entityType.GetProperty("Id"));
I also define a navigation property in the Order class and want to set up a referential constraint:
NavigationPropertyConfiguration nav = orderEntityTypeConfig.AddNavigationProperty(customerPropertyInfo, EdmMultiplicity.ZeroOrOne);
orderEntitySetConfig.AddBinding(nav, customerEntitySetConfig);
nav.HasConstraint(customerIdPropertyFromOrderClass, idPropertyFromCustomerClass);
With this setup I get the following exception:
System.ArgumentException: "Cannot redefine property 'Id' already defined on the base type 'Module.BusinessObjects.Person'. It does not matter whether the ReflectedType of the principal PropertyInfo is Customer or Persion - I tried it both ways.
Here is the stack trace:
> Microsoft.AspNet.OData.dll!Microsoft.AspNet.OData.Builder.StructuralTypeConfiguration.ValidatePropertyNotAlreadyDefinedInBaseTypes(System.Reflection.PropertyInfo propertyInfo) Zeile 674 C#
Microsoft.AspNet.OData.dll!Microsoft.AspNet.OData.Builder.StructuralTypeConfiguration.AddProperty(System.Reflection.PropertyInfo propertyInfo) Zeile 309 C#
Microsoft.AspNet.OData.dll!Microsoft.AspNet.OData.Builder.NavigationPropertyConfiguration.HasConstraint(System.Collections.Generic.KeyValuePair<System.Reflection.PropertyInfo, System.Reflection.PropertyInfo> constraint) Zeile 256 C#
Microsoft.AspNet.OData.dll!Microsoft.AspNet.OData.Builder.NavigationPropertyConfiguration.HasConstraint(System.Reflection.PropertyInfo dependentPropertyInfo, System.Reflection.PropertyInfo principalPropertyInfo) Zeile 221 C#
If I just remove the refererential constraint then the model is setup correctly and the service works as expected.
If I remove the inheritance for testing purposes and I define the key in the Customer class instead, then the constraint works fine as well.
How do I define this constraint correctly?
Thanks and regards
Bernd