使用控制器进行数据迁移的ASP.NET Code First迁移

时间:2014-04-10 01:51:49

标签: c# asp.net entity-framework asp.net-identity ef-migrations

我正在使用Identity 2.0和Entity Framework 6.0构建ASP.NET MVC 5 Web应用程序,并且正在使用代码优先迁移。我在使用自定义身份验证系统(不是.NET)的现有数据库上构建它,我希望将数据从现有用户表迁移到新的身份表(AspNetUser和AspNetClaims)。

我有一个InitialCreate迁移,它创建了新表,我希望在迁移运行时使用现有用户表中的数据填充这些表。我不想只使用带有Identity 2.0的现有表,所以不要只使用[Table(“oldtablename”]属性来装饰模型。

我知道Up()方法中有Sql()方法,但是我想使用自动创建的Identity类 - AccountController-> Register(RegisterViewModel) - 而不是正确的哈希,guids,在AspNetUser表中创建。 UserDetails模型包含旧表中的用户数据(用户名,密码[plain-text ..eek])。

以下是我的代码:

 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)
                })
            }
}

但是,当我运行Update-Database以应用迁移时,虽然新的AspNet表已成功创建,但不会在AspNetUsers表中创建新用户。 从Package Console Manager运行时,DbContext不可用吗?或者发生了什么?我离开它3年后才重新回到ASP.NET上,所以我还在试图找出新架构。

Package Manager控制台的输出是:

    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.

1 个答案:

答案 0 :(得分:0)

感谢您的回复。

我确实在散列密码时遇到错误,但我发现错误是由某种超时错误引起的,因为当我再次运行它而没有改变任何东西时(重新启动visual studio后)它运行得很好。