Remove Auto Increment in EF Core 1.0 RC2 (former EF 7 RC2)

c# ef-fluent-api entity-framework-core sqlite


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

            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);
7/26/2018 9:38:52 AM

Accepted Answer

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();
5/23/2017 12:17:51 PM

Popular Answer

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

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow