【发布时间】:2015-10-26 22:54:20
【问题描述】:
我尝试为 Member 实体做一个主键,我不想使用注解,只使用 fluent API:
Member{
public string MemberID {get;set;}
...
}
在成员映射中
this.hasKey(t=>t.MemberID);
更新数据库时出现此错误:
Identity column 'MemberID' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with
a scale of 0, and constrained to be nonnullable
【问题讨论】:
-
默认情况下,EF 希望其 PK 列不为空。由于 string 不是值类型,因此它可以为 null。 PK 也必须是某些类型,我认为字符串不是其中之一。另外,EF会使PK的自增,与字符串类型不匹配。
标签: string entity-framework ef-code-first migration primary-key