Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run EF Core migrations on SQL Server database using Docker?

On a Mac I have a Net Core 2.1 Class Library with a few Entity Framework migrations.

I need to use Docker to run SQL Server and update the database with the previews migrations.

I added a docker-compose.yml file to my project:

docker-compose.yml
src
  data
    project.data.csproj

The docker-compose.yml file is the following:

services:
  data:
    build: .
    depends_on:
      - database
  database:
  image: "microsoft/mssql-server-linux"
  environment:
    SA_PASSWORD: "Pass.word123"
    ACCEPT_EULA: "Y"

At the moment this is not working.

How can I run EF Core migrations on SQL Server using docker in a Mac?

like image 960
Miguel Moura Avatar asked Nov 08 '18 01:11

Miguel Moura


1 Answers

You need to add below lines in Confiure of startup file. It will executed when the application is started. It will update all the migration files to database.

using (IServiceScope scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetService<MyDBCOntext>().Database.Migrate();
            }

This worked for me.

like image 154
Gopi Avatar answered Oct 20 '22 08:10

Gopi