24

Some time ago, I created a ASP.NET MVC 5 website with the Identity 1.0 version, and i created the Identity tables with this project. Now i have to make other website using the same database for authentication, but now the Identity version is 2.0. So when i try to authenticate in the new website i get some errors.

Im trying to migrate the database using the Migrations approach, but its confused and im getting this error There is already an object named 'AspNetRoles' in the database. when i type Update-Database in the PM console.

My question is, how is the best way to use the same database for the authetication of both sites (one using the 1.0 identity version and other using 2.0). Do I really need to migrate the database?

If yes, how can i solve this error that im getting?

gog
  • 11,788
  • 23
  • 67
  • 129

7 Answers7

43
Add-Migration InitialMigrations -IgnoreChanges

This should generate a blank "InitialMigration" file. Now, add any desired changes to the class you want. Once changes are added, run the update command again:

update-database -verbose

Now the automatic migration will be applied and the table will be altered with your changes.

Edit: Here is a solution to migrate identity 1 to 2 Upgrading from ASP.NET.Identity 1.0 to 2.0 Use this manual migration

public override void Up()
    {
        RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
        RenameIndex(table: "dbo.AspNetUserClaims", name: "IX_User_Id", newName: "IX_UserId");
        DropPrimaryKey("dbo.AspNetUserLogins");
        AddColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 256));
        AddColumn("dbo.AspNetUsers", "EmailConfirmed", c => c.Boolean(nullable: false));
        AddColumn("dbo.AspNetUsers", "PhoneNumber", c => c.String()); 
        AddColumn("dbo.AspNetUsers", "PhoneNumberConfirmed", c => c.Boolean(nullable: false));
        AddColumn("dbo.AspNetUsers", "TwoFactorEnabled", c => c.Boolean(nullable: false));
        AddColumn("dbo.AspNetUsers", "LockoutEndDateUtc", c => c.DateTime());
        AddColumn("dbo.AspNetUsers", "LockoutEnabled", c => c.Boolean(nullable: false));
        AddColumn("dbo.AspNetUsers", "AccessFailedCount", c => c.Int(nullable: false));
        AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256));
        AlterColumn("dbo.AspNetUsers", "FirstName", c => c.String(nullable: false));
        AlterColumn("dbo.AspNetUsers", "LastName", c => c.String(nullable: false));
        AddColumn("dbo.AspNetUsers", "CreatedDateTime", c => c.DateTime(nullable: false));
        AlterColumn("dbo.AspNetRoles", "Name", c => c.String(nullable: false, maxLength: 256));
        AddPrimaryKey("dbo.AspNetUserLogins", new[] { "LoginProvider", "ProviderKey", "UserId" });
        CreateIndex("dbo.AspNetUsers", "UserName", unique: true, name: "UserNameIndex");
        CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex");
        DropColumn("dbo.AspNetUsers", "Discriminator");
    } 
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
  • thanks! now i not getting this error, but im getting this ""Invalid column name 'Email'.\r\nInvalid column name 'EmailConfirmed'.\r\nInvalid column name 'PhoneNumber'." when i try to login. Theres other changes i need to do in this User Model? Im using the IdentityUser without custom properties. – gog Jun 11 '14 at 18:59
  • Here is a solution to migrate identity 1 to 2 [link]http://adamstephensen.com/2014/05/02/upgrading-from-asp-net-identity-1-0-to-2-0/ – Mohsen Esmailpour Jun 11 '14 at 19:13
  • 6
    I just get `A parameter cannot be found that matches parameter name 'IgnoreChanges'.` in EF Core – Bassie Dec 02 '18 at 13:00
  • This solution is for EF 6 and I don’t know it works with EF Core. – Mohsen Esmailpour Dec 02 '18 at 13:10
  • This worked for me like a charm I am using mvc5 with EF6 – Ninja Dec 05 '20 at 15:05
13

While you can (since EF6) use migrations in two separate projects for the same database, there can't be any overlap. The way migrations work is through a dbo._MigrationHistory table that stores the context that generated the migration and the model state of your application, which includes the Identity models.

When you try to connect your second application, it finds no previous migrations, and thus needs to generate it's initial migration, which will include tables for the Identity models, which are in its context as well. That's where your problem is.

For the purposes of Identity, you need to choose one project to make the master. This one will utilize a standard IdentityDbContext where the Identity models will be migrated.

The other project will need to be made a slave, at least in terms of utilizing Identity. So, you will need to interact with at least two contexts in this application. One will be a subclass of IdentityDbContext, but treated as database-first:

public class MyIdentityContext : IdentityDbContext<ApplicationUser>
{
    public MyIdentityContext()
        : base("ConnectionStringNameForYourSharedDB")
    {
        Database.SetInitializer<MyIdentityContext>(null);
    }
}

The other context will be just a regular DbContext subclass that will be migrated as normal. You'll need to repeat this for any other project that may need access to the same Identity information from the same database. Also, because of the repetitious code this will lead to (and the fact that your ApplicationUser class will need to be shared) you should move this code into a class library that each project can reference.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
6

In the "appsettings.json" file change the name of database

"ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-S0S1I;Database=New_name_here;Trusted_Connection=True;"

and then

add-migration 
update-database

It works.

Saharsh
  • 750
  • 7
  • 18
2

For me, the problem couldn't be fixed by adding an empty migration. My problem was that the __EFMigrationsHistory table had been manually cleared. As a consequence, the context.Database.Migrate() method thought it had to apply all the missing migrations (missing entries in __EFMigrationsHistory). Simply add all the missing migrations in the db table and it should work again.

Jimmy
  • 864
  • 13
  • 24
1

for that first we have to remove migration that we newly migrated dotnet ef migrations remove -s ..\InvoiceManagement\

then delete *.Designer.cs file in the migration folder

finally dotnet ef database update -s ..\InvoiceManagement\ -s mean that startup project(webApi) remember that this code shoud be use in DBcontext file contain folder path. this code use in vscode for webapi.

pamal Sahan
  • 451
  • 3
  • 7
0

I faced the same bug as below. Then I fixed it as below: (.NET Core / EF Core)

  1. Check current databases in your project:

    dotnet ef migrations list
    
  2. If the newest is what you've added, then remove it:

    dotnet ef migrations remove
    
  3. Guarantee outputs of this database must be deteled in source code:

    .cs/.Designer.cs files

  4. Now it is fine. Try to re-add:

    dotnet ef migrations add [new_dbo_name]
    
  5. Finally, try to update again, in arrangement base on migration list:

    dotnet ef database update [First]
    dotnet ef database update [Second]
    ...
    dotnet ef database update [new_dbo_name]
    

Hope it is helpful for you.

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Mai Nguyen
  • 21
  • 4
-1

I have faced the same error.

If you have saved any ConnectionString name in the "Environment Variables" of your device and using the same ConnectionString name again, it generates this kind of error, try to use a different ConnectionString name

In my case, it was this kind of issue!!!