.net core 2 with ef core migrations with docker in development .net core 2 with ef core migrations with docker in development docker docker

.net core 2 with ef core migrations with docker in development


If your IP is working and name is not. Then try giving full name of the DBServer e.g. DBServer.domain.xyz


You can implement IDesignTimeDbContextFactory<TContext> which is used by dotnet ef command line tools. Create a file called DesignTimeDbContextFactory in your asp.net core project (I placed it in the Data folder). The content should be:

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDBContext>{    public MyDBContext CreateDbContext(string[] args)    {        IConfigurationRoot configuration = new ConfigurationBuilder()            .SetBasePath(Directory.GetCurrentDirectory())            .AddJsonFile("appsettings.json")            .Build();        var builder = new DbContextOptionsBuilder<MyDBContext>();        var connectionString = configuration.GetConnectionString("migrationContextConnection");        builder.UseSqlServer(connectionString);        return new MyDBContext(builder.Options);    }}

And then add the following connection string to appsettings.json

  "ConnectionStrings": {    "migrationContextConnection": "Server=localhost;Database=HSRDC;User=sa;Password=SFMasc32*;"  }

Now you have to expose the port from your SQL docker container so that it's accessible by the host system. Add the following to your docker compose:

version: '3'services:  db:    image: microsoft/mssql-server-windows-express    environment:      sa_password: "pwd"      ACCEPT_EULA: "Y"    networks:      - testnetwork    ports:      - "1433:1433" 

You can now run dotnet ef database update and it'll work.