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

Identity Column Not Applied When Updating Database #6459

Closed
kieranhoney opened this issue Sep 2, 2016 · 4 comments
Closed

Identity Column Not Applied When Updating Database #6459

kieranhoney opened this issue Sep 2, 2016 · 4 comments

Comments

@kieranhoney
Copy link

kieranhoney commented Sep 2, 2016

Steps to reproduce

Unfortunately this is part of a much larger project. However I will provide anything I can.

The issue

Forgive me if this shouldn't be logged here or I have done something stupid, publishing migrations doesn't seem to apply the Identity flag to a column but only on Sql Server. I'm using OpenIddict with Identity to generate the base schema for the database. A table AspNetUserClaims has an Identity column that is shown with the correct annotation (I believe) in the migration script:

migrationBuilder.CreateTable(
    name: "AspNetUserClaims",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("Autoincrement", true),
        ClaimType = table.Column<string>(nullable: true),
        ClaimValue = table.Column<string>(nullable: true),
        UserId = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
        table.ForeignKey(
            name: "FK_AspNetUserClaims_AspNetUsers_UserId",
            column: x => x.UserId,
            principalTable: "AspNetUsers",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

My startup.cs is switching between a Dev and Prod environment, Dev is using Sqlite and Prod is using a DB on a Sql Server instance. When I apply this migration to the database using dotnet ef database update or by generating a script directly for each environment, using:

dotnet ef migrations script -e Development -o Sqlite.sql
dotnet ef migrations script -e Production -o SqlServer.sql

Sqlite (Dev) gives:

CREATE TABLE "AspNetUserClaims" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_AspNetUserClaims" PRIMARY KEY AUTOINCREMENT,
    "ClaimType" TEXT,
    "ClaimValue" TEXT,
    "UserId" TEXT NOT NULL,
    CONSTRAINT "FK_AspNetUserClaims_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
);

I have little knowledge on Sqlite but I see the AUTOINCREMENT attribute is there, however the Sql Server (Production) script/update gives the following:

CREATE TABLE [AspNetUserClaims] (
    [Id] int NOT NULL,
    [ClaimType] nvarchar(max),
    [ClaimValue] nvarchar(max),
    [UserId] uniqueidentifier NOT NULL,
    CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
);

There is no IDENTITY modifier on the [Id] field, nor are there any constraints defined elsewhere in the script.

Exception message:

Generic exception about attempting to place a NULL value in the AspNetUserClaims [ID] field.
Stack trace:

Further technical details

EF Core version: (found in project.json or packages.config)
Microsoft.EntityFrameworkCore.Sqlite: 1.0.0
Microsoft.EntityFrameworkCore.SqlServer: "1.0.0
Microsoft.EntityFrameworkCore.Tools: 1.0.0-preview2-final

Operating system:
Windows Server 2008 R2

Visual Studio version: (e.g. VS 2013 or n/a)
Visual Studio 2015 Professional

Other details about my project setup:
None of relevance as far as I'm aware.

@kieranhoney
Copy link
Author

Apologies but I have no idea how to format the code correctly.

@bricelam
Copy link
Contributor

bricelam commented Sep 2, 2016

Migrations are currently only generated based on the active provider (SQLite). You'll need to compensate for any additional providers you intend to run the migrations on. After the autoincrement annotation, add the following.

.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)

Getting the tools to do this for you automatically is issue #1825

@divega
Copy link
Contributor

divega commented Sep 2, 2016

EF Team Triage: closing as dupe of #1825.

@divega divega closed this as completed Sep 2, 2016
@kieranhoney
Copy link
Author

Thank you, I apologise for my ignorance.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants