返回 TypeMismatchRelationshipConstraint 的 WCF 元数据

WCF metadata returning TypeMismatchRelationshipConstraint

我有一个 WCF 项目使用 entity framework 5 EDMX 引用另一个 DAL 项目。

我无法检索我的服务的元数据:http://localhost:12034/DataService.svc/$metadata

它返回以下错误:

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...

我基本上有以下关系

ResourceType table 在 TenantID + ResourceTypeID 上有 PK 资源 table 在 TenantID + ResourceID 上有 PK,在 ResourceType 上有 FK。

这似乎影响了我框架上的所有 FK,因为如果我删除当前受影响的关系,它将发生在另一个关系上。

个别实体显示正确:http://localhost:12034/DataService.svc/ResourceTypes

在您的数据库中,检查资源 table 上 ResourceType 的数据类型,并验证它与 ResourceType table 上的 ResourceTypeID 的数据类型相同。对 TenantID 字段执行相同操作。

只要它们相同,从 table 更新您的 EDMX,然后右键单击您的 EDMX 文件并单击 "Add Code Generation Item" 并通过保存替换当前的 .tt 文件您正在生成的新文件与当前文件同名,并选择覆盖现有文件。这基本上会刷新您对 table 及其关系的 EDMX 引用。

这可能会解决问题。 EDMX 很挑剔,如果您没有按照正确的步骤正确更新,有时您会 运行 遇到这些问题。

这似乎是 WCF 数据服务与复合键顺序的问题。相关主题:Getting Metadata TypeMismatchRelationshipConstraint error for WCF Data Services project.

键在数据服务类型中排序:ResourceType,然后传播到实体类型,但在 navigationProperty 的依赖属性中未排序。

解决方案是禁用案例的模型验证,例如:

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

或者,尝试一下 OData Web API's 5.4 beta which has Referential Constraint support, or RESTier