Asp.net-Mvc-4
ASP.NET SimpleMembershipProvider 的自動遷移
所以我嘗試在我的新 MVC 4 項目中使用自動遷移,但不知何故它不起作用。我一步一步地關注了這篇博文。
我已將更改添加到
UserProfile帳戶模型(NotaryCode欄位):[Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public int NotaryCode { get; set; } }然後我在包管理器控制台上寫了
enable-migrations一個配置類(從 繼承DbMigrationsConfiguration<Web.Models.UsersContext>)然後我將類填充為:public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(Atomic.Vesper.Cloud.Web.Models.UsersContext context) { WebSecurity.InitializeDatabaseConnection( "DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!Roles.RoleExists("Atomic")) Roles.CreateRole("Atomic"); if (!Roles.RoleExists("Protocolista")) Roles.CreateRole("Protocolista"); if (!Roles.RoleExists("Cliente")) Roles.CreateRole("Cliente"); string adminUser = "randolf"; if (!WebSecurity.UserExists(adminUser)) WebSecurity.CreateUserAndAccount( adminUser, "12345", new { NotaryCode = -1 }); if (!Roles.GetRolesForUser(adminUser).Contains("Atomic")) Roles.AddUsersToRoles(new[] { adminUser }, new[] { "Atomic" }); }然後我嘗試執行
update-database -verbose,但這不起作用。我的意思是,這是輸出:數據庫中已經有一個名為“UserProfile”的對象。
PM> update-database -verbose Using StartUp project 'Web'. Using NuGet project 'Web'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Target database is: 'VesperCloud' (DataSource: .\SQLSERVER, Provider: System.Data.SqlClient, Origin: Configuration). No pending code-based migrations. Applying automatic migration: 201211051825098_AutomaticMigration. CREATE TABLE [dbo].[UserProfile] ( [UserId] [int] NOT NULL IDENTITY, [UserName] [nvarchar](max), [NotaryCode] [int] NOT NULL, CONSTRAINT [PK_dbo.UserProfile] PRIMARY KEY ([UserId]) ) System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'UserProfile' in the database. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) 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, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) 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.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:a7da0ddb-bccf-490f-bc1e-ecd2eb4eab04 **There is already an object named 'UserProfile' in the database.**我知道對象存在。我的意思是,我嘗試使用自動遷移來精確地修改和執行,而無需手動重新創建數據庫。但不知何故,這不起作用。
我查看了 MSDN 文件並找到了該屬性:
AutomaticMigrationDataLossAllowed = true;但是將其設置為 true 並不會改變任何事情。我想我錯過了一些東西,但不知何故找不到什麼。任何想法?
update-database -verbose不起作用,因為在您的數據表已經存在之後您的模型已更改。首先,確保 UserProfile 類沒有更改。然後,執行:
Add-Migration InitialMigrations -IgnoreChanges這應該會生成一個空白的“InitialMigration”文件。現在,向 UserProfile 類添加任何所需的更改。添加更改後,再次執行更新命令:
update-database -verbose現在將應用自動遷移,並且表格將隨著您的更改而更改。