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?
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);
});
...
...
...
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With