简体   繁体   English

在Entity Framework数据库迁移期间应如何运行自定义代码?

[英]How should custom code be run during an Entity Framework database migration?

I have a very simple Entity Framework 5 DbMigration that is adding a new boolean column to an existing table with thousands of records in it. 我有一个非常简单的Entity Framework 5 DbMigration,它向具有数千条记录的现有表中添加了一个新的布尔列。 I want the initial value of that column for each existing row to be set based on the value in two other columns in the same table. 我希望基于同一表中其他两个列中的值来设置每个现有行的该列的初始值。 This should only affect existing records, therefore should only be run when the migration is performed and never run again. 这只会影响现有记录,因此仅应在执行迁移时运行,而不再运行。

This is basically the logic that needs to be executed: 这基本上是需要执行的逻辑:

var users = (from u in context.Users select u).ToList();
users.ForEach(u =>
{
    u.MyNewBoolColumn = (u.Column1 != null && u.Column2 == "some-value");
    context.Users.AddOrUpdate(u);
});

There are two options I can think of, but I don't like either of them: 我可以想到两种选择,但我都不喜欢其中一种:

  1. Create two separate migrations, since the column won't exist until after the first one is completed. 创建两个单独的迁移,因为直到第一个迁移完成后该列才存在。 This seems sloppy and I'm also not sure how to get the database context in the second migration to actually perform the update. 这似乎草率,而且我也不确定如何在第二次迁移中获取数据库上下文以实际执行更新。
  2. Run code in a DbMigrationsConfiguration implementation. 在DbMigrationsConfiguration实现中运行代码。 However, this code would run every single time and I won't be able to tell if it has already run and shouldn't update records. 但是,此代码将每次运行一次,而我将无法判断它是否已经运行并且不应该更新记录。

Is there another, better option? 还有其他更好的选择吗?

A migration represents a change in the schema , therefor you can't use AddOrUpdate() stuff inside a migration. 迁移表示schema的更改,因此您不能在迁移内使用AddOrUpdate()东西。 But you can, and this is what I would suggest you, run plain SQL code using the Sql() method. 但是可以,并且这就是我建议您使用Sql()方法运行纯SQL代码的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不同的数据库上运行实体框架代码优先迁移 - Run entity framework code first migration on different database MySql数据库中的实体框架代码优先迁移 - Entity Framework Code First Migration in MySql DataBase 在实体框架迁移期间读取数据库(选择查询) - Read database during an entity framework migration (select query) 如何使用 Entity Framework Core 在加密的 SQLite 数据库上运行迁移? - How can I run a migration with Entity Framework Core on an encrypted SQLite database? 使用自定义连接字符串进行代码优先实体框架迁移 - Using a custom connection string for Code First Entity Framework Migration 实体框架代码优先迁移始终尝试创建新数据库 - Entity framework code first migration always tries to create new database 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 实体框架6.0代码优先迁移-模型/数据库兼容性错误? - Entity Framework 6.0 Code First Migration - Model/Database compatibility bug? 实体框架核心更新数据库 - 无需迁移的代码优先方法 - Entity Framework Core Update Database - Code First Approach Without Migration 实体框架代码第一次自动迁移不适用于 Oracle 数据库 - Entity Framework code first auto migration not working with Oracle database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM