本文介绍了删除EF Core 1.0 RC2(以前的EF 7 RC2)中的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Entity Framework Core 1.0 RC2(以前的Entity Framework 7 RC2)中,默认情况下,所有整数主键均为自动增量字段。我尝试了所有删除操作。从使用数据注释到流畅的API,什么都行不通。
In Entity Framework Core 1.0 RC2 (former Entity Framework 7 RC2), by default, all integer primary key are auto increment field. I tried everything to remove it. From using data annotation to fluent API, nothing works.
使用数据注释:
[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
As Requested, here is the table script that I get after running 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 Core中,。
In EF Core, key and property are configured separately.
指定密钥:
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId);
配置属性为不自动递增:
To configure the property not being auto increment:
modelBuilder.Entity<tblProduct>().Property(t => t.ProdId).ValueGeneratedNever();
这篇关于删除EF Core 1.0 RC2(以前的EF 7 RC2)中的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!