Question

I am building an ASP.NET MVC 5 Web Application with Identity 2.0 and Entity Framework 6.0 and am using Code-First Migrations. I am building it on an existing database that used a custom authentication system (not .NET) and I wish to migrate data from the existing user tables to the new Identity tables (AspNetUser and AspNetClaims).

I have an InitialCreate migration which creates the new tables, and I would like to populate these tables with data from my existing user tables when the migration runs. I do not want to just use the existing tables with Identity 2.0, so don't want to just decorate the model with [Table("oldtablename"] attributes.

I know that there is the Sql() method available in the Up() method, but I would like to use the automatically created Identity classes - AccountController->Register(RegisterViewModel) - instead so the proper hashes, guids, are created in the AspNetUser table. The UserDetails model contains the user data from the old tables (username, password [plain-text ..eek]).

The following is my code:

 public partial class InitialCreate : DbMigration
    {

    public override void Up() {

..// Create new AspNetTables etc...

      // Migrate records from old classic asp authentication to ASP.NET Identity
            ApplicationDbContext udc = new ApplicationDbContext();
            var users = udc.UserDetails;

            AccountController ac = new AccountController();
            foreach(UserDetails u in users) {

                ac.Register(new RegisterViewModel { 
                    UserName = u.UserLogin, 
                    Password = Crypto.HashPassword(u.UserPass),
                    ConfirmPassword = Crypto.HashPassword(u.UserPass)
                })
            }
}

However, when I run Update-Database to apply the migration, the new users are not created in the AspNetUsers table, though the new AspNet tables are created successfully. Is the DbContext not available when running from Package Console Manager? Or what is happening? I am just getting back on ASP.NET after 3 years off it, so I'm trying to still figure out the new architecture.

The output from Package Manager Console is:

    Applying explicit migrations: [201404090639371_InitialCreate].
Applying explicit migration: 201404090639371_InitialCreate.
System.Runtime.Remoting.RemotingException: Object '/a5a2f6bb_b6a4_4de9_9439_d0abfcf7efe3/i8xnyqr81tjkwllqwr1npgaj_501.rem' has been disconnected or does not exist at the server.
   at System.Data.Entity.Migrations.Design.ToolingFacade.ToolLogger.Verbose(String sql)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32.<ExecuteStatements>b__2e()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object '/a5a2f6bb_b6a4_4de9_9439_d0abfcf7efe3/i8xnyqr81tjkwllqwr1npgaj_501.rem' has been disconnected or does not exist at the server.
Was it helpful?

Solution

Thanks for the reply.

I did have an error in hashing the passwords, but I found the error was caused by some kind of timeout error, as when I ran it again without changing anything (after restarting visual studio) it ran perfectly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top