ASP.NET
0
Вклад в тег
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 для миграций
}
}
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.")));
}