[英]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.