EF 4.3.1迁移播种未按预期工作 [英] EF 4.3.1 Migrations Seeding not working as expected

查看:72
本文介绍了EF 4.3.1迁移播种未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF 4.3.1迁移,并且具有其中有以下代码的Configuration类:

I am using EF 4.3.1 migrations and I have the Configuration class in which I have the following code:

internal sealed class Configuration : DbMigrationsConfiguration<DbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(PayByPhoneDbContext context)
    {
        context.Roles.AddOrUpdate(r => r.Name, new Role { Name = "A" }, new Role { Name = "B" });
        context.Administrators.AddOrUpdate(a => a.Email, new Administrator { Email = "a@a.com" Name = "A", Role = context.Roles.Local.SingleOrDefault(role => role.Name == "A") });
    }
}

现在,当我运行migration命令时(MSBuild的一部分)脚本)(如果脚本不存在),则创建表并按预期进行播种。但是,当我在不进行任何迁移的情况下在现有数据库上运行migration命令并且所有数据都已植入种子时(需要进行更新而不是插入时)时,我在运行migration命令时遇到错误:

Now when I run the migrate command (part of MSBuild script) when the DB does not exist, tables are created and the seeding takes place as expected. But when I run the migrate command on an existing database without any migrations and all data is already seeded (when update needs to occur instead of insert) I get an error on running the migrate command:


没有待执行的显式迁移。

No pending explicit migrations.

运行种子方法。

System.Data.Entity.Infrastructure.DbUpdateException:
更新条目时发生错误。有关详细信息,请参见内部异常。 --->
System.Data.UpdateException:更新
条目时发生错误。有关详细信息,请参见内部异常。 --->
System.Data.SqlClient.SqlException:UPDATE语句使
与FOREIGN KEY约束 FK_Administrators_Roles_RoleId发生冲突。
冲突发生在数据库 xxxDB的表 dbo.Roles的 Id列中。

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Administrators_Roles_RoleId". The conflict occurred in database "xxxDB", table "dbo.Roles", column 'Id'.

该语句已终止。

Stacktrace:

Stacktrace:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   --- End of inner exception stack trace ---
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   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()

运行SQL Server分析器,我发现执行该错误:

Running SQL Server profiler I found the error occurred when executing:

exec sp_executesql N'update [dbo].[Administrators]
set [RoleId] = @0
where ([Id] = @1)
',N'@0 int,@1 bigint',@0=0,@1=1

正确的方法是

推荐答案

AddOrUpdate会将未指定的列重置为null(或零)

AddOrUpdate will reset columns which you don't specify to null (or zero in the case of an int).

我的理解是,这种行为(这很糟糕)不适用于它被识别为键或列的列自动增加,但显然是您的情况。

My understanding was that this behavior (which is awful by the way) wouldn't apply to columns that it recognized as keys or as auto-increment, but apparently it is in your case.

我的建议是进行检查以确保该角色在数据库中不存在,然后执行一个标准的add()(如果没有) t。

My advice would be to do a check to make sure the role doesn't exist in the database and then do a standard add() if it doesn't.

我对所有种子数据都执行此操作,因为您永远不知道什么时候以后可以提供一个界面来更新您的种子数据,并且您不希望种子()方法来覆盖在这种情况下的更改。

I do this for all my seed data because you never know when you may provide an interface later for updating data you seeded, and you don't want the Seed() method to overwrite your changes in those circumstances.

还请记住,如果您选择使用MigrateDatabaseToLatestVersion数据库初始化程序,则Seed()将在每次您的应用程序重新启动(dll部署,web.config更新等)。

Also keep in mind that if you ever choose to use the MigrateDatabaseToLatestVersion database initializer then Seed() will fire each time your app restarts (dll deployment, web.config update, etc).

最好在Seed()方法中进行防御性编码。

It's best to code defensively in the Seed() method.

更新:朱莉·勒曼(Julie Lerman)的有关此行为的博客文章

Update: Julie Lerman has a good blog post about this behavior.

这篇关于EF 4.3.1迁移播种未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆