entity-framework - EF 核心 : Add custom LINQ in Up method of Migration class

标签 entity-framework entity-framework-core

我想在迁移的 Up 方法中执行一些 LINQ。问题是我不知道如何获取 DbContext 实例?

这是由 migrations add 生成的代码:

public partial class MyTableAddFieldTitle : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<string>(
            name: "Title",
            table: "MyTable",
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "Title",
            table: "MyTable");
    }
}

我想在 Up 方法中添加类似的东西:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "Title",
        table: "MyTable",
        nullable: true);

    var context = ?????;

    //Actual code is much more complicated, but the principle is the same.
    foreach (var item in context.Set<DbMyTable>())
      item.Title = item.SomeStringColumn;
    context.SaveChanges();
}

问题是如何获取上下文实例?我在构造函数中尝试使用 DI:

protected MyTableAddFieldTitle(MyContext context)
{
}

但我得到错误:

MissingMethodException: No parameterless constructor defined for this object. System.RuntimeTypeHandle.CreateInstance(RuntimeType type, bool publicOnly, ref bool canBeCached, ref RuntimeMethodHandleInternal ctor)

最佳答案

我找到了解决方案。

Startup 类中我定义了静态变量:

public static Func<MyContext> ContextFactory;

Startup 类的构造函数中,我分配了变量:

public Startup(IHostingEnvironment env, IConfiguration config)
{
    MyContext GetContext(IConfiguration configuration, IHostingEnvironment environment)
    {
        var builder = new DbContextOptionsBuilder<MyContext>();
        builder.UseSqlServer(configuration["ConnectionStrings:Web"], b => b.MigrationsAssembly("Web.Hosting"));
        if (environment.IsDevelopment())
            builder.EnableSensitiveDataLogging();
        return new MyContext(builder.Options);
    }

    ContextFactory = () => GetContext(config, env);
}

然后在迁移中,我只需调用 ContextFactory:

var context = Startup.ContextFactory();
context.Set<DbMyTable>().Where(....

为避免错误字段不存在,我创建了 2 个迁移文件(dotnet ef migrations add)。
首先添加字段:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "Title",
        table: "MyTable",
        nullable: true);
}

第二个(空)执行查询:

protected override void Up(MigrationBuilder migrationBuilder)
{
    var context = Startup.ContextFactory();
    context.Set<DbMyTable>().Where(....
}

关于entity-framework - EF 核心 : Add custom LINQ in Up method of Migration class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49400742/

相关文章:

entity-framework - 使用虚拟属性更新类时, Entity Framework 验证失败

c# - Entity Framework 代码优先使用一列作为主键,另一列作为自增列

entity-framework - 循环/遍历所有EF模型中的所有属性以设置列类型

entity-framework - SQLite 数据库放置在 UWP 应用中的位置

c# - 在 ASP.NET Core 和 EF Core 中使用 MySQL 提供程序时出现 NotImplemented 异常

c# - 在 1 对 0..1 关系中对对象删除实现参照完整性

具有不同名称的相同数据库表的 C# 匿名类型

c# - 检查实体是否在 Code First 中的其他实体中有引用

entity-framework - EF Core 内连接而不是左连接

c# - Skip and Take 不适用于 MySQL EntityFrameworkCore