问题
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.
Using data annotation:
[Key, Column(Order = 1, TypeName = "INT"), DatabaseGenerated(DatabaseGeneratedOption.None)]
Using fluent 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);
Nothing has worked :(
Can you please help me?
UPDATED
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);
});
...
...
...
回答1:
In EF Core, key and property are configured separately.
To specify the key:
modelBuilder.Entity<tblProduct>().HasKey(t => t.ProdId);
To configure the property not being auto increment:
modelBuilder.Entity<tblProduct>().Property(t => t.ProdId).ValueGeneratedNever();
回答2:
I did not work with EF 7 but few points to check
- After you changed the model you need to update database, migration or manually
- Check in database if you have now autoincrement on field
来源:https://stackoverflow.com/questions/41908391/remove-auto-increment-in-ef-core-1-0-rc2-former-ef-7-rc2