Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 ASP.NET SimpleMembershipProvider的自动迁移_Asp.net Mvc 4_Entity Framework 5_Entity Framework Migrations - Fatal编程技术网

Asp.net mvc 4 ASP.NET SimpleMembershipProvider的自动迁移

Asp.net mvc 4 ASP.NET SimpleMembershipProvider的自动迁移,asp.net-mvc-4,entity-framework-5,entity-framework-migrations,Asp.net Mvc 4,Entity Framework 5,Entity Framework Migrations,因此,我尝试在我的新MVC4项目中使用自动迁移,但不知何故它不起作用。我一步一步地走 我已将更改添加到UserProfile帐户模型(NotaryCode字段): 然后我在package manager控制台上编写了启用迁移,出现了一个配置类(继承自DbMigrationsConfiguration),然后我将该类填充为: public Configuration() { AutomaticMigrationsEnabled = true; } protected override v

因此,我尝试在我的新MVC4项目中使用自动迁移,但不知何故它不起作用。我一步一步地走

我已将更改添加到
UserProfile
帐户模型(NotaryCode字段):

然后我在package manager控制台上编写了
启用迁移
,出现了一个配置类(继承自
DbMigrationsConfiguration
),然后我将该类填充为:

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" });
}
然后我试着运行
updatedatabase-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;

但将其设置为真并不能改变任何事情。我想我错过了什么,但不知怎么的,我没有找到什么。有什么想法吗?

这里发生的情况是您启用了迁移,然后运行了应用程序。通过在使用UpdateDatabase命令之前运行应用程序,EntityFramework将创建并填充数据库,但由于启用迁移时数据库不存在,因此不会创建InitialCreate迁移。迁移仍然认为您的数据库是空的,并且希望在模型中创建所有对象


您可以尝试重新启用迁移,这将生成反映数据库当前状态的InitialCreate迁移。在本例中,我将保存您对seed方法所做的更改,然后运行“enablemigrations-Force”,这将重新创建迁移并生成一个initalcreate迁移。然后可以重新填充seed方法并运行UpdateDatabase命令

更新数据库-verbose
不起作用,因为在数据表已经存在之后,模型已经更改

首先,确保UserProfile类没有更改。然后,运行:

添加迁移初始迁移-忽略更改

这将生成一个空白的“InitialMigration”文件。现在,向UserProfile类添加任何所需的更改。添加更改后,再次运行update命令:


更新数据库-详细


现在将应用自动迁移,并且表将随您的更改而更改。

我有相同的,并以不同的方式排序。转到我的本地数据库删除了UserProfile和其他具有外键约束的表webpages\u Membership、webpages\u OAuthMembership、webpages\u Roles、webpages\u UsersInRoles表。当您运行updatedatabase-verbose时,所有这些都将重新创建。

实际上,这非常有效。不幸的是,我只能给你一个+1和公认的答案。你帮我省去了很多麻烦。再次感谢。接下来,我是否应该始终使用添加迁移初始迁移?或者这只是第一次。我在网上找不到关于它的任何文件。不客气。您只需运行一次就可以创建一个InitialMigrations类。您通过一个简单的选项“IgnoreChanges”解决了我2天的问题。我应该能够点击100000次投票。如果我得到了<代码>,数据库中已经有一个名为“ASPNETRe饰件”的对象。< /代码>我添加到空白>代码>初始迁移文件中有什么变化?虽然我不明白为什么我需要使用AddMigrationInitialMigrations-IgnoreChanges创建初始迁移文件,但我删除了数据库并重新创建了它。有人能解释一下吗?我只能在添加空的初始迁移文件时更新数据库。请查看我的答案,希望这有帮助。。。
AutomaticMigrationDataLossAllowed = true;