Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
            });
...
...
...
like image 817
Sam Avatar asked Dec 02 '22 13:12

Sam


1 Answers

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();
like image 78
Ivan Stoev Avatar answered Dec 25 '22 17:12

Ivan Stoev