I have a .Net Core WebApplication Project in which the Context Class is in a Class Library. If I hard code the connection string in the OnConfiguring(DbContextOptionsBuilder
This bit me hard, getting errors like:
No database provider has been configured for this DbContext.No design-time services were found.The server was not found or was not accessible.But I ended up with a fairly simple solution/work-around:
in your Startup.cs add the migration-project:
public void ConfigureServices(IServiceCollection services)
{
var myDbContextAssemblyName = typeof(MyDbContext).Assembly.GetName().Name;
var connectionString = Configuration.GetConnectionString(MyDbContext.ConnectionStringName);
services.AddDbContext(options => options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly(myDbContextAssemblyName)));
// do more ...
}
in your connection-string use the IP-address and port-number (inspired by this corefx issue), instead of the server-name/dns (FWIW: query to get IP).
So now I have this in my appsettings.Development.json:
"ConnectionStrings": { "MyConnectionStringName": "Data Source=10.1.2.3,1433; Initial Catalog=MyCatalog; Integrated Security=SSPI" }
I found a lot of other suggestions, and I will mention a few that seemed interesting. Maybe it will help someone else:
Mention startup-project and migration-project in command-line:
Update-Database -Verbose -Project x.Data -StartupProject x.Web
One can also call migrations at StartUp , "for apps with a local database". (I guess otherwise running on multiple nodes, may start multiple runtime migrations at the same time with concurrency issues?)
myDbContext.Database.Migrate();
This EntityFrameworkCore issue states:
The problem is that when EF calls either
CreateWebHostBuilderorBuildWebHostit does so without runningMain. (This is intentional because EF needs to build the model and use theDbContextwithout starting the application.) This means that when EF invokes on of these methods the staticIConfigurationproperty is still null--since it is only set inMain. So, you'll need to either make sure thatIConfigurationis set/handled when EF calls one of these methods, or useIDesignTimeDbContextFactory.
This is not necessary for me, I guess because .Net Core 2 loads the configuration behind the scenes.
This EntityFrameworkCore issue states:
The typical way to do this is to read a file, an environment variable, or similar inside
IDesignTimeDbContextFactory.
This seems too much a hack to me.
Microsoft documentation mentions:
Some of the EF Core Tools commands (for example, the Migrations commands) require a derived DbContext instance to be created at design time in order to gather details about the application's entity types and how they map to a database schema.
They mention these ways to provide this design time DbContext:
from application services: for an ASP.NET Core app as startup project:
The tools try to obtain the
DbContextobject from the application's service provider. [...] The tools first try to obtain the service provider by invokingProgram.BuildWebHost()[JP: orCreateWebHostBuilder] and accessing theIWebHost.Servicesproperty. TheDbContextitself and any dependencies in its constructor need to be registered as services in the application's service provider. This can be easily achieved by having a constructor on theDbContextthat takes an instance ofDbContextOptionsas an argument and using theAddDbContextmethod.
Using a constructor with no parameters
If the
DbContextcan't be obtained from the application service provider, the tools look for the derivedDbContexttype inside the project. Then they try to create an instance using a constructor with no parameters. This can be the default constructor if theDbContextis configured using theOnConfiguringmethod.
From a design-time factory
You can also tell the tools how to create your
DbContextby implementing theIDesignTimeDbContextFactoryinterface: If a class implementing this interface is found in either the same project as the derived DbContext or in the application's startup project, the tools bypass the other ways of creating theDbContextand use the design-time factory.