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

Viewed 3446

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?

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.

Related