Вот мое решение
public class DockerContext: DbContext
{
private bool InDocker { get { return Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true"; } }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!InDocker)
{
// Здесь переопределяем ConnectionString для миграций
}
}
или так
program.cs
public static bool InDocker { get { return Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true"; } }
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DockerContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString(InDocker ? "DockerContext" : "MigrationContext") ?? throw new InvalidOperationException("Connection string 'DockerContext' not found.")));
}