Oleg Sh
Oleg Sh

Reputation: 9013

Update ASP.NET Identity

I have a project with ASP.NET Identity 2.0 and DB, created by ASP.NET Identity 1.0. I want to update my DB. I do the following actions:

  1. Enable migrations:

Enable-Migrations -ProjectName Web

Checking if the context targets an existing database...

Code First Migrations enabled for project Web.

  1. Add Migration (My project has 2 connection strings, I select appropriate, with 'old' DB tables):

Add-Migration -Name UpdateIdentity -ProjectName Web -ConnectionStringName MainContext

Scaffolding migration 'UpdateIdentity'.

The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration UpdateIdentity' again.

but it generates the following script:

public override void Up()
{
    CreateTable(
        "dbo.AspNetRoles",
        c => new
            {
                Id = c.String(nullable: false, maxLength: 128),
                Name = c.String(nullable: false, maxLength: 256),
            })
        .PrimaryKey(t => t.Id)
        .Index(t => t.Name, unique: true, name: "RoleNameIndex");

    CreateTable(
        "dbo.AspNetUserRoles",
        c => new
            {
                UserId = c.String(nullable: false, maxLength: 128),
                RoleId = c.String(nullable: false, maxLength: 128),
            })
        .PrimaryKey(t => new { t.UserId, t.RoleId })
        .ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId)
        .Index(t => t.RoleId);

    CreateTable(
        "dbo.AspNetUsers",
        c => new
            {
                Id = c.String(nullable: false, maxLength: 128),
                CompanyId = c.Int(nullable: false),
                SMSnumber = c.String(),
                FullName = c.String(),
                Email = c.String(maxLength: 256),
                EmailConfirmed = c.Boolean(nullable: false),
                PasswordHash = c.String(),
                SecurityStamp = c.String(),
                PhoneNumber = c.String(),
                PhoneNumberConfirmed = c.Boolean(nullable: false),
                TwoFactorEnabled = c.Boolean(nullable: false),
                LockoutEndDateUtc = c.DateTime(),
                LockoutEnabled = c.Boolean(nullable: false),
                AccessFailedCount = c.Int(nullable: false),
                UserName = c.String(nullable: false, maxLength: 256),
            })
        .PrimaryKey(t => t.Id)
        .Index(t => t.UserName, unique: true, name: "UserNameIndex");

    CreateTable(
        "dbo.AspNetUserClaims",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                UserId = c.String(nullable: false, maxLength: 128),
                ClaimType = c.String(),
                ClaimValue = c.String(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId);

    CreateTable(
        "dbo.AspNetUserLogins",
        c => new
            {
                LoginProvider = c.String(nullable: false, maxLength: 128),
                ProviderKey = c.String(nullable: false, maxLength: 128),
                UserId = c.String(nullable: false, maxLength: 128),
            })
        .PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
        .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
        .Index(t => t.UserId);

}

It tries to create tables from zero, but I need a script to modify. Why so and how to create correct scripts?

Upvotes: 1

Views: 316

Answers (1)

Steve Greene
Steve Greene

Reputation: 12304

EF does a compare to the prior migration. If there is none, it compares the current models to an empty database. So what you need is to create an initial, codeless migration:

add-migration MyStartPoint -IgnoreChanges

Now the next migration will be the changes only. So in your case, if you can roll back to Identity 1, make this migration, update to Identity 2, add a 2nd migration then you will get what you need. See here.

Upvotes: 2

Related Questions