Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? I'd like to run dotnet ef database update against my build artefacts without need of project.json and source codes.

In other words I'm looking for an equivalent of migrate.exe https://msdn.microsoft.com/en-us/data/jj618307.aspx from EF6

like image 467
krlm Avatar asked Oct 17 '16 10:10

krlm


People also ask

Can I use ef core without migration?

If you'd like to access data from an existing database and tables with Entity Framework (EF) Core in your ASP.NET Core Web API project, you can try to use Scaffold-DbContext command or dotnet ef dbcontext scaffold command to generate code for a DbContext and entity types for your database.

How do I run down ef core migration?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

How do I enable migrations on my ef core?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.

How do I run an existing migration in Entity Framework?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.


2 Answers

My team colleague found a way which allows you to run migrations on build artefacts without sources. Following command replace migrate.exe for us:

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:

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 ./
like image 181
krlm Avatar answered Oct 19 '22 15:10

krlm


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).

One option to update database without use dotnet ef database update is execute the command bellow in some default action or startup routine.

_dbContext.Database.Migrate();
like image 20
Ricardo Fontana Avatar answered Oct 19 '22 16:10

Ricardo Fontana