2

I would like to execute migration in separate environments. I'm working with Entity Framework and ASP.NET Core. For now I have this :

public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        builder.UseSqlServer("Data Source=.\\LOCALHOST;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=true");

        return new ApplicationDbContext(builder.Options);
    }
}

This code is in a DAL class library who separate the database interactions from the server code.

It's ok to use this when executing migrations but I don't really like doing it like this.

The problem here is that I use a local database for development but we also have a development server and a staging server with different connection strings. What I can't manage to do is to find a way to not hardcode the connection strings in that class.

Is there a way to configure the connection strings for different environments and indicate which to use when executing the migrations ?

2 Answers 2

2

Yes, you can use environment variables for this.

Thats why you have appsettings.<EnvironmentName>.json

Configuration in ASP.NET Core
Not bad Microsoft article abount Working with multiple environments


FYI
Environment.GetEnvironmentVariable

Sign up to request clarification or add additional context in comments.

3 Comments

I knew that I can configure it like this, that's what i did but how can I specify which environment I'm in when executing migration ?
@AntoineThiry Set ASPNETCORE_ENVIRONMENT before executing migration
Thanks, I missed that part, shouldn't have ask this obvious question...
1

You need to inject DbContextOptionsBuilder<ApplicationDbContext> into your factory constructor and then configure the DbContext in the main application's Startup.cs

public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    private readonly DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder;

    public TemporaryDbContextFactory(DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder)
    {
        if(optionsBuilder==null)
            throw new ArgumentNullException(nameof(optionsBuilder));

        this.optionsBuilder = optionsBuiler;
    }

    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

And in Startup.cs:

services.AddDbContext<ApplicationDbContext>(options => {
    options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"));
});

Configuration.GetConnectionString("MyConnectionString") is shorthand for Configuration["ConnectionStrings:MyConnectionString"].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.