删除EF Core 1.0 RC2中的自动增量(以前的EF 7 RC2)

时间:2017-01-28 09:47:30

标签: c# sqlite entity-framework-core ef-fluent-api

在Entity Framework Core 1.0 RC2(以前的Entity Framework 7 RC2)中,默认情况下,所有整数主键都是自动增量字段。我尝试了一切来删除它。从使用数据注释到流畅的API,没有任何作用。

使用数据注释:

[Key, Column(Order = 1, TypeName = "INT"), DatabaseGenerated(DatabaseGeneratedOption.None)]

使用流利的API:

modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("DatabaseGenerated", DatabaseGeneratedOption.None);

//OR use the following
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("DatabaseGenerated", 0);

//OR use the following
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId).HasAnnotation("Sqlite:Autoincrement", false);

没有任何效果:(

你能帮帮我吗?

已更新

如已请求,这是我在运行添加迁移LocalDB_v1

后获得的表脚本
migrationBuilder.CreateTable(
            name: "tblProduct",
            columns: table => new
            {
                ProdId = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Name = table.Column<string>(nullable: true),
                Description = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_tblProduct", x => x.ProdId);
            });
...
...
...

2 个答案:

答案 0 :(得分:12)

在EF Core中,d3

指定密钥:

modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId);

配置属性不是自动增量:

modelBuilder.Entity<tblProduct>().Property(t => t.ProdId).ValueGeneratedNever();

答案 1 :(得分:0)

我没有使用EF 7,但很少检查

  • 更改模型后,您需要更新数据库,迁移或手动
  • 如果您现在在字段
  • 上有自动增量,请检入数据库