Entity Framework 4.1 代码优先 KeyAttribute 作为非身份列

发布于 2024-11-03 14:00:52 字数 505 浏览 3 评论 0原文

我的代码优先模型有问题。数据现在位于数据库中,因此我无法使用 DropCreateDatabaseIfModelChanges 类重新设定数据库种子,但我需要更改一个表,以便 bigint 列不是 IDENTITY(1,1)。我已经设法使用 SSMS 做到这一点,但现在我的 EF 代码说它已经过时了。 这是相关表的代码:

public class Vote {
    [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long FacebookUserId { get; set; }
    [Required]
    public Entity Entity { get; set; }
}

所以我更改了我的表架构和模型(我认为这是它的反映,但我显然错了),但 EF 仍然说我的模型已超出日期,我无法重新播种数据库以使其“完美”。

任何帮助将不胜感激。

谢谢,

本杰明

I've got a problem with a code-first model I've got. Data is now in the database so I can't re-seed the database using a DropCreateDatabaseIfModelChanges class, but I need to change one table so that a bigint column is not an IDENTITY(1,1). I've managed to do this using SSMS but now my EF code is saying it's out of date.
This is the code for the table in question:

public class Vote {
    [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long FacebookUserId { get; set; }
    [Required]
    public Entity Entity { get; set; }
}

So I've changed my table schema, and my model (which I thought was the reflection of it, but I'm obviously wrong), but EF is still saying my model is out of date, and I can't re-seed the database to get it "perfect".

Any help would be much appreciated.

Thanks,

Benjamin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

撑一把青伞 2024-11-10 14:00:53

尝试将其添加到您的 OnModelCreating 中:

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

这应该会消除模型已过期的异常,但在此之前您必须始终手动同步模型和数据库。

Try add this to your OnModelCreating:

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

That should remove the exception that model is out of date but till this time you must always synchronize model and database manually.

桃扇骨 2024-11-10 14:00:52

使用数据注释:

public class Customer
{
[Key]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CustomerID { get; set; }
}

使用 Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>().Property(c => c.CustomerID).HasDatabaseGeneratedOption(null);
    base.OnModelCreating(modelBuilder);
}

Using data annotation:

public class Customer
{
[Key]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CustomerID { get; set; }
}

Using fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>().Property(c => c.CustomerID).HasDatabaseGeneratedOption(null);
    base.OnModelCreating(modelBuilder);
}
绳情 2024-11-10 14:00:52

参考这篇文章 ...

似乎实体框架默认情况下期望您插入到标识列中。

要解决这个问题,请尝试

    modelBuilder.Entity<BOB>()
    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

builder.Entity<BOB>().MapSingleType().ToTable("BOB");

或用以下内容装饰 POCO 中的密钥...

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.None)]
 public Int64 PolicyID { get; set; }

referring to this post ...

It seems that entity framework expects by default that you insert into identity column.

to solve this try

    modelBuilder.Entity<BOB>()
    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

builder.Entity<BOB>().MapSingleType().ToTable("BOB");

or decorate your key in the POCO with ...

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.None)]
 public Int64 PolicyID { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文