简体   繁体   English

实体框架代码优先 - 非主键字段的外键

[英]Entity Framework Code First - Foreign Key to non primary key field

I have two tables that look like this: 我有两个看起来像这样的表:

dbo.ReviewType
    ReviewTypeId INT PRIMARY KEY
    ShortName CHAR(1) - Unique Index
    Description

dbo.Review
   ReviewId INT PRIMARY KEY
   ReviewType_ShortName CHAR(1) - FK to ReviewType
   ...

A Review always has a ReviewType. 评论始终具有ReviewType。
A ReviewType can be associated with many reviews. ReviewType可以与许多评论相关联。

I'm having trouble mapping this in Entity Framework using the Code First Fluent API. 我在使用Code First Fluent API在Entity Framework中映射它时遇到了麻烦。 It seems like it does not like me using a foreign key that doesn't map to the Primary Key. 看起来它不喜欢我使用不映射到主键的外键。 I'm using a foreign key to a Unique Constraint/Index instead of to the Primary Key. 我使用外键到Unique Constraint / Index而不是Primary Key。

How can I map this properly in Entity Framework using C#? 如何使用C#在Entity Framework中正确映射?

I should note that the way I am doing it right now is giving me this error: 我应该注意到我现在这样做的方式是给我这个错误:

System.Data.Edm.EdmAssociationConstraint: : 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. System.Data.Edm.EdmAssociationConstraint ::参照约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同。 The type of property 'ReviewTypeCode' on entity Review' does not match the type of property 'Id' on entity 'ReviewType' in the referential constraint 'ReviewType_Reviews'. 实体Review'上的属性'ReviewTypeCode'的类型与引用约束'ReviewType_Reviews'中实体'ReviewType'上的属性'Id'的类型不匹配。

当前版本的EF不支持唯一索引,也不能基于非主要唯一键映射关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先使用具有另一个表的非主键的外键 - Entity Framework Code First approach foreign key with non primary key of another table 实体框架外键作为主键代码优先 - Entity Framework Foreign Key as Primary Key Code First 在带有外行键字段的代码优先迁移的Entity Framework 7中添加索引 - Adding an Index in Entity Framework 7 with Code First Migrations on a field that is a Foreign Key Entity Framework 5.0非主键的复合外键-可能吗? - Entity Framework 5.0 composite foreign key to non primary key - is it possible? 实体框架6.1.3将外键映射到非主键 - Entity Framework 6.1.3 Mapping Foreign key to non primary key 违反主要密钥实体框架代码 - Violation of primary key Entity Framework Code First 首先删除实体框架代码中的主键 - deleting primary key in entity framework code first 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table 在Code-First Entity Framework中包含复合主键中的外键 - Include foreign key in composite primary key in Code-First Entity Framework 代码生成:向实体框架添加主键/外键 - Code Generation : Add primary key / foreign key to Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM