自动生成主键(Guid)实体框架CTP5

发布于 2024-10-31 04:03:31 字数 535 浏览 2 评论 0原文

我有一个以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶花眉 2024-11-07 04:03:31

你不应该:

    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid AccountId { set; get; }

Shouldn't you have:

    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid AccountId { set; get; }

?

最偏执的依靠 2024-11-07 04:03:31

杰夫的回答是正确的。只是给你一个小提示。使用 EF6,我编写了下一个配置,以便设置名称为“Id”的所有字段并键入“Guid”作为身份。

modelBuilder.Properties<Guid>()
           .Where(info => info.Name.ToLower()== "id")
           .Configure(configuration => configuration.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity));

所以我不必每次都编写 [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.

modelBuilder.Properties<Guid>()
           .Where(info => info.Name.ToLower()== "id")
           .Configure(configuration => configuration.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity));

So I don't have to write [DatabaseGenerated(DatabaseGenerationOption.Identity)] everytime.

不可一世的女人 2024-11-07 04:03:31

听起来您必须将您的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文