Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold-DbContext not correctly configuring auto incrementing primary key in from Sqlite database #11961

Closed
DanNeely opened this issue May 10, 2018 · 7 comments · Fixed by #20063
Assignees
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-2.2 punted-for-3.0 type-bug
Milestone

Comments

@DanNeely
Copy link

DanNeely commented May 10, 2018

I'm using EF.Core scaffold to create a datamodel for a DB with an auto increment primary key but the context is being generated with the key annotated with ValueGeneratedNever() instead of ValueGeneratedOnAdd(). As a result EF framework requires each record to have a primary key set instead of allowing the DB to set it.

Steps to reproduce

Create an SQLite DB with this table:

CREATE TABLE "LastSyncTime" 
( 
    `ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
    `SyncTime` TEXT NOT NULL, 
    `TableUpdated` TEXT NOT NULL 
)

In the package manager console run:

Scaffold-DbContext "DataSource=MyDatabase.sqlite;" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models -force

Examine the context class, it generates with:

modelBuilder.Entity<LastSyncTime>(entity =>
{
    entity.HasIndex(e => e.Id)
        .IsUnique();

    entity.Property(e => e.Id)
        .HasColumnName("ID")
        .ValueGeneratedNever();

    entity.Property(e => e.SyncTime).IsRequired();

    entity.Property(e => e.TableUpdated).IsRequired();
});

ValueGeneratedNever() should be ValueGeneratedOnAdd()

Further technical details

EF Core version: (found in project.csproj or packages.config) 2.1.0-rc1-final (also happens in 2.0.2)
Database Provider: (e.g. Microsoft.EntityFrameworkCore.Sqlite)
Operating system: Win 10 1709
IDE: (e.g. Visual Studio 2017 15.6.3)

@ajcvickers ajcvickers added this to the 2.2.0 milestone May 11, 2018
@Joe4evr
Copy link

Joe4evr commented May 12, 2018

Possibly related, also in SQLite. I found out that with my entity:

public class Account
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int Balance { get; set; }
}

giving Balance a default value in OnModelCreating:

modelBuilder.Entity<Account>()
    .Property(ac => ac.Balance)
    .HasDefaultValue(1500);

scaffolds the following in a migration:

migrationBuilder.CreateTable(
    name: "Accounts",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        Balance = table.Column<int>(nullable: false, defaultValue: 1500)
            .Annotation("Sqlite:Autoincrement", true) //WTF?
    });

Why does the scaffolding think that the second column needs auto-increment?

Commenting out the bit in OnModelCreating doesn't emit the annotation for the Balance column to the migration scaffold.

@jjung89
Copy link

jjung89 commented May 28, 2018

I have the same problem with GUID.
[Id] UNIQUEIDENTIFIER NOT NULL

@chrisbecke
Copy link

Ran into the same issue with EFCore 2.1.2

dotnet ef dbcontext scaffold ... -f -d -v

renders a PRIMARY KEY AUTOINCREMENT field as a ValueGeneratedNever in the generated DbContext, and the reverse operation

dotnet ef migrations add ...

generates PRIMARY KEY AUTOINCREMENT columns in the SQLite db where the property either has no ValueGenerated... at all, or is explicitly ValueGeneratedNever().

Looking forward to 2.2.0 I guess.

@ajcvickers ajcvickers modified the milestones: 2.2.0-preview2, 2.2.0 Sep 11, 2018
@ajcvickers ajcvickers modified the milestones: 2.2.0, 3.0.0 Oct 1, 2018
@emyfreya
Copy link

emyfreya commented Apr 9, 2019

Still no fixes for this ValueGeneratedNever() problem ?

@jsnells1
Copy link

jsnells1 commented Oct 8, 2019

Any updates on this?

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 8, 2019

Willing to take a PR for this?

@bricelam
Copy link
Contributor

bricelam commented Dec 9, 2019

@ErikEJ Of course! It might depend on #10228

@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 25, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview2 Mar 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview2, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-2.2 punted-for-3.0 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants