自动生成主键(Guid)实体框架CTP5
我有一个以下 POCO 类,
public class Account
{
[Key,DatabaseGenerated(DatabaseGenerationOption.Identity)]
public string AccountId { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
public string Email { set; get; }
}
创建数据库时出现以下异常
Identity column 'AccountId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.
I have a following POCO class
public class Account
{
[Key,DatabaseGenerated(DatabaseGenerationOption.Identity)]
public string AccountId { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
public string Email { set; get; }
}
I get the following exception when the database gets created
Identity column 'AccountId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不应该:
?
Shouldn't you have:
?
杰夫的回答是正确的。只是给你一个小提示。使用 EF6,我编写了下一个配置,以便设置名称为“Id”的所有字段并键入“Guid”作为身份。
所以我不必每次都编写
[DatabaseGenerate(DatabaseGenerationOption.Identity)]
。Jeff's answer is right. Just a little tip for you. Using EF6 I wrote next configuration in order to set all fields with name "Id" and type "Guid" as identity.
So I don't have to write
[DatabaseGenerated(DatabaseGenerationOption.Identity)]
everytime.听起来您必须将您的 AccountId 属性从字符串更新为异常中提到的数据类型之一:
int、bigint、smallint、tinyint 或小数位数或小数位数为 0,并且限制为不可空的
任何原因它现在是细绳?
Sound like you have to update your AccountId property from string to one of the mentioned datatypes in the exception:
int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable
Any reason why it's now a string?