简体   繁体   English

EF Core - 无源运行迁移 - 等效于 EF6 的 migrate.exe

[英]EF Core - Running migrations without sources - Equivalent of EF6's migrate.exe

Is it possible to run an ef migration from DLL containing migrations and dbcontext?是否可以从包含迁移和 dbcontext 的 DLL 运行 ef 迁移? I'd like to run dotnet ef database update against my build artefacts without need of project.json and source codes.我想针对我的构建工件运行dotnet ef database update ,而无需 project.json 和源代码。

In other words I'm looking for an equivalent of migrate.exe https://msdn.microsoft.com/en-us/data/jj618307.aspx from EF6换句话说,我正在寻找来自 EF6 的migrate.exe https://msdn.microsoft.com/en-us/data/jj618307.aspx

My team colleague found a way which allows you to run migrations on build artefacts without sources.我的团队同事找到了一种方法,可以让您在没有源的情况下对构建工件运行迁移。 Following command replace migrate.exe for us:以下命令为我们替换migrate.exe

dotnet exec 
  --runtimeconfig ./HOST.runtimeconfig.json 
  --depsfile ./HOST.deps.json Microsoft.EntityFrameworkCore.Design.dll
  --assembly ./DB_CONTEXT_DLL.dll 
  --startup-assembly ./HOST.dll --data-dir ./ 
  --root-namespace DB_CONTEXT_NAMESPACE 
  --verbose database update --context DB_CONTEXT_CLASS -e development 

Update for 2.1.x version: 2.1.x 版本更新:

dotnet exec 
   --runtimeconfig ./HOST.runtimeconfig.json 
   --depsfile ./HOST.deps.json /PATH/TO/microsoft.entityframeworkcore.tools/.../ef.dll 
   --verbose database update --context DB_CONTEXT_CLASS
   --assembly ./DB_CONTEXT_DLL.dll 
   --startup-assembly ./HOST.dll --data-dir ./

Seems not possible run dotnet ef database update only with the DLL, and if you use the docker, the actual version of runtime microsoft/dotnet:1.1.0-preview1-runtime do not have the sdk installed (with the dotnet ef database update command).似乎不可能只用 DLL 运行dotnet ef database update ,如果你使用microsoft/dotnet:1.1.0-preview1-runtime ,运行时的实际版本microsoft/dotnet:1.1.0-preview1-runtime没有安装 sdk(使用dotnet ef database update命令)。

One option to update database without use dotnet ef database update is execute the command bellow in some default action or startup routine.在不使用dotnet ef database update情况下更新数据库的一种选择是在某些默认操作或启动例程中执行以下命令。

_dbContext.Database.Migrate();

I happen to use a factory to obtain a context, so based on Richardo's answer created a class like this.我碰巧使用工厂来获取上下文,因此根据 Richardo 的回答创建了一个这样的类。 I use it as a singleton and call its ApplyMigrations method at service startup.我将它用作单例并在服务启动时调用它的 ApplyMigrations 方法。

using System;
using Microsoft.EntityFrameworkCore;

namespace Nhs.Digital.Cwt.MultiStreamPublisher
{
    public class MigrationApplier
    {
        private IMyContextFactory _contextFactory;

        public MigrationApplier(IMyContextFactory contextFactory)
        {
            _contextFactory = contextFactory ?? throw new ArgumentNullException($"{nameof(contextFactory)} was null");
        }

        public void ApplyMigrations()
        {
            if (_contextFactory != null)
            {
                using (var context = _contextFactory.Create())
                {
                    _contextFactory = null;
                    context.Database.Migrate();
                }
            }
        }
    }
}

Damn ran into problems though as my deployment scripts expected the DB to be there so that DB permissions can be added to it.该死的遇到了问题,因为我的部署脚本希望数据库在那里,以便可以向其中添加数据库权限。 So it looks like I'll have to write a small console app just for the deploy script to use to make the DB ahead of adding DB permissions.所以看起来我必须为部署脚本编写一个小型控制台应用程序,以便在添加数据库权限之前创建数据库。 Code on through the pain.通过痛苦编码。

I just created a small utility to solve this problem.我刚刚创建了一个小实用程序来解决这个问题。 It will works with any providers out of the box.它将与任何开箱即用的提供程序一起使用。 The only requirements is your assembly must have a public implementation of IDesignTimeDbContextFactory .唯一的要求是您的程序集必须具有IDesignTimeDbContextFactory的公共实现。

The usage is very simple, just execute the src/EFMigrate project with the first argument is a path to your published assembly.用法很简单,只需执行src/EFMigrate项目,第一个参数是您发布的程序集的路径。

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

相关问题 Entity Framework Core 和 EF6 - dotnet ef 迁移添加 InitialCreate - 值不能为空。 (参数“连接字符串”) - Entity Framework Core and EF6 - dotnet ef migrations add InitialCreate - Value cannot be null. (Parameter 'connectionString') 没有Web / App.config运行实体框架migrate.exe工具的正确格式是什么? - What is the correct format for Running Entity Framework migrate.exe tool without a Web/App.config? SAP HANA数据库是否支持EF6迁移? - Are EF6 migrations supported on SAP HANA database? EF6 Enable-Migrations在ASP.NET Core(.NET Framework)中不起作用 - EF6 Enable-Migrations not working in ASP.NET Core (.NET Framework) ef6 .net核心与现有数据库 - ef6 .net core with existing database 在不创建额外迁移文件的情况下将 ef 迁移到 ef 核心的最佳方法 - Best way to migrate ef to ef core without creating extra migration files EF6引发错误,从另一台计算机的共享文件夹中调用exe - EF6 throws error, invoking exe from different machine's shared folder 不选择MySQL的情况下更新对象 - Update Object without Select EF6 MySQL VSTS中完整.net项目中的EF核心迁移 - Ef core migrations in full .net project in VSTS 是否可以在不使用machine.config的情况下为migrate.exe配置自定义提供程序 - Is it possible to configure a custom provider for migrate.exe without using the machine.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM