4

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);
            });
...
...
...
Deilan
  • 4,740
  • 3
  • 39
  • 52
Sam
  • 1,826
  • 26
  • 58
  • Can you post table script ? – Vova Bilyachat Jan 28 '17 at 09:48
  • After you changed the model have you generated migration? – Vova Bilyachat Jan 28 '17 at 09:49
  • @VolodymyrBilyachat - I have updated my question as requested. Yes, after I change my model, I delete my existing migration and snapshot and rerun **add-migration LocalDB_v1** – Sam Jan 28 '17 at 09:59
  • as you said that it works with RC1 then i would update to that version and post bug to github – Vova Bilyachat Jan 28 '17 at 10:00
  • Get updated. Currently there is EF Core v1.1.0 release, staying on RC (old) versions imposes limitations and no fixes. – Ivan Stoev Jan 28 '17 at 12:28
  • @IvanStoev, it is all the latest. **Microsoft.EntityFrameworkCore** v1.1.0, **Microsoft.EntityFrameworkCore.Sqlite** v1.1.0, **Microsoft.EntityFrameworkCore.Tools** v1.1.0-preview4-final – Sam Jan 28 '17 at 12:33
  • @Sam. Oh? So why don't you add the right tags and do you refer to EF7 RC2? Does this also apply to your previous quesions? – Gert Arnold Jan 28 '17 at 18:59
  • @GertArnold - what tag would you suggest? There is no EF7 RC2. There is only **entity-framework-core**. Please let me know what tag would you recommend and I will change the tag. Thanks – Sam Jan 28 '17 at 22:47
  • Do we understand each other? You say you're on the latest version, so it's not EF7 RC2 and you should use the ef-core tag. – Gert Arnold Jan 29 '17 at 19:00
  • @GertArnold - sorry Gret, I don't quite understand. I thought EF7 RC2 = EF-Core? I did put **entity-framework-core** tag. There is no **ef-core** tag. – Sam Jan 30 '17 at 10:19
  • Yes EF7-RC2 = ef-core (RC2). I.e. it was renamed. So don't use the term EF7 anymore. https://blogs.msdn.microsoft.com/dotnet/2016/05/16/announcing-entity-framework-core-rc2/ – Gert Arnold Jan 30 '17 at 11:45

2 Answers2

18

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();
Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • sadly, this didn't work for me; for some reason the ValueGeneratedNever() method doesn't exist, but HasDatabaseGeneratedOption(DatabaseGeneratedOption.None) does. I used that and it made as much difference as using the Data Annotations. – therightstuff Apr 25 '17 at 23:44
0

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
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • It also happen with brand new project. EF 7 RC1 was fine, but RC2 generates all auto increment for integer primary. I want to remove this auto increment field. Furthermore, I don't need to update the database (ie. no backward compatibility needed) as my project is still under development. – Sam Jan 28 '17 at 09:58
  • @Sam then try to update to RC1, and write bug in github – Vova Bilyachat Jan 28 '17 at 09:59
  • 2
    @RC1 is fine because in RC1 by default it is **NOT** auto increment field. Ok, will write to the github... it will be my first time writing to github. lol... – Sam Jan 28 '17 at 10:04