Search code examples
c#entity-frameworkvisual-studio-2012odatawcf-data-services

WCF metadata returning TypeMismatchRelationshipConstraint


I have a WCF project referring another DAL project with entity framework 5 EDMX.

I can't retrieve the metadata of my service : http://localhost:12034/DataService.svc/$metadata

Its returning following error :

An IEdmModel instance was found that failed validation. The following errors were reported: TypeMismatchRelationshipConstraint : The types of all properties in the dependent role of a referential constraint must be the same as the corresponding property types in the principal role. The type of property 'TenantID' on entity 'PScopeModel.Resource' does not match the type of property 'ResourceTypeID' on entity 'ResourceType' in the referential constraint 'Fred'.

TypeMismatchRelationshipConstraint : The types of all properties in the dependent role of a referential constraint must be the same as the corresponding property types in the principal role. The type of property 'ResourceType' on entity 'PScopeModel.Resource' does not match the type of property 'TenantID' on entity 'ResourceType' in the referential constraint 'Fred'.

TypeMismatchRelationshipConstraint : The types of all properties in the dependent role of a referential constraint must be the same as the corresponding property types in the principal role. The type of property 'TenantID' on entity 'PScopeModel.Resource' does not match the ty...

I basically have following relationship Relationship

ResourceType table has PK on TenantID + ResourceTypeID Resource table has PK on TenantID + ResourceID and FK on ResourceType.

This seems to be affecting all FKs on my framework, since if I remove the current impacted relation, it will occur on another relationship.

Individual entities are displayed correctly : http://localhost:12034/DataService.svc/ResourceTypes


Solution

  • That seems an issue of WCF Data Services with the composite key's order. A related thread: Getting Metadata TypeMismatchRelationshipConstraint error for WCF Data Services project.

    Key sorted in data service type: ResourceType, and then propagated to entity type, but not sorted in navigationProperty's dependent properties.

    A solution is to disable the model validation for the case, e.g.:

    public static void InitializeService(DataServiceConfiguration config)
    {
        ...
        config.DisableValidationOnMetadataWrite = true;
    

    Or, give it a try about OData Web API's 5.4 beta which has Referential Constraint support, or RESTier.