[英]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.