在 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);
没有任何效果:(
你能帮帮我吗?
已更新
根据要求,这是我在运行 add-migration 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);
});
...
...
...
最佳答案
在 EF 核心中,key and property are configured separately .
指定 key :
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId);
配置属性不自增:
modelBuilder.Entity<tblProduct>().Property(t => t.ProdId).ValueGeneratedNever();
关于c# - 删除 EF Core 1.0 RC2(以前的 EF 7 RC2)中的自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41908391/