11

I have been ranging across multiple questions, tutorials and examples for something that fits this problem.

What if I don't know my connection string at the time I want to create my first initial migration? Given I am given the opportunity to set the connection string at the time of instantiating context eg:

var connection = @"Server=(localdb)\mssqllocaldb;Database=JobsLedgerDB;Trusted_Connection=True;ConnectRetryCount=0";
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlServer(connection);

using (var context = new BloggingContext(optionsBuilder.Options))
{
  // do stuff
}

As described in the docs..

If you need to have a connection string to run migrations then for those situations where you don't have one (Tenant database where you get a connection string from a user account) how do you run an initial migration??

Do you create a dummy connection string to create the migration.. seems dodgy to me. I would love some input on this.

6
  • I'm not sure if I understood your question correctly. But you do need to test out your app and so at some point of time you would need to have a working database, right? For development I'd normally use my local DB (SQL Server 2017), and the connection string would be pointing to the local server. Commented May 5, 2019 at 4:52
  • 1
    Possible duplicate of EF core - Creating a migration for a database without a connection string and in another class library Commented May 6, 2019 at 9:28
  • 1
    I suugest you edit the previous question instead of asking basically the same again. Commented May 6, 2019 at 9:29
  • 1
    Did you find something about this? I was also adding migrations with a dummy connection string, but when it comes to Remove-Migration, I guess it does not accept a dummy string, it fails. Commented Jan 24, 2020 at 21:39
  • 1
    how did you fixed it, I've got the same issue Commented Feb 11, 2021 at 3:29

1 Answer 1

1

You can implement IDesignTimeDbContextFactory that will be used instead your host builder in the design time. UseSqlServer now has a parameterless overload.

It will look like this and has to be in the same assembly as the db context or in the startup project:

public class MyAppDbContextFactory: IDesignTimeDbContextFactory<MyAppDbContext>
{
    public MyAppDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyAppDbContext>();
        optionsBuilder.UseSqlServer();
            
        return new MyAppDbContext(optionsBuilder.Options);
    }
}

More details can be found here: https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory

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

2 Comments

In my case, EF generates only one statement in a SQL script - creating "__EFMigrationsHistory" table.
What about poor peasants like me who don't use Microsoft SQL Server, but MariaDB or similar with the "Pomelo.EntityFrameworkCore.MySql" package?

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.