Randeep Singh
Randeep Singh

Reputation: 1018

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

Upvotes: 1

Views: 237

Answers (2)

Congyong Su
Congyong Su

Reputation: 338

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.

Upvotes: 3

Justin Russo
Justin Russo

Reputation: 2214

In your db, check the datatype of ResourceType on the Resource table, and verify it's the same datatype of the ResourceTypeID on the ResourceType table. Do the same for the TenantID field.

As long as those are the same, update your EDMX from the table, then, right click in your EDMX file and click, "Add Code Generation Item" and replace your current .tt files by saving the new ones you are generating as the same names as your current files and electing to overwrite existing. This will basically refresh your EDMX references to the tables and their relations.

This may fix the problem. EDMX is finicky and if you don't follow the steps properly and update correctly, you will sometimes run into these issues.

Upvotes: 0

Related Questions