I\'ve got a Solution with many projects.
One of them (Domain) is a .NET Standard 2.0 project where I made my EF Core DbContext implementation for w
Runtime and SDK from here - I guess you need .NET Core 2.1.302 at the momentMicrosoft.EntityFrameworkCore.SqlServer.Design is not needed anymore as it's included to SDK. CLI reference in csproj fiels for EntityFrameworkCore is not needed as well.Manage NuGet packages window shows all updated. Add anywhere in you web project implementation of IDesignTimeDbContextFactory interface - it will be found automatically and used for EF Add-Migration (or dotnet ef... analogues) command in Package Manager Console
public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory
{
public ActivitiesDbContext CreateDbContext(string[] args)
{
DbContextOptionsBuilder builder = new DbContextOptionsBuilder();
var context = new ActivitiesDbContext(
builder
.UseSqlServer("Data Source=(local)\LocalDB;Initial Catalog=DB_name;Integrated Security=True;")
.Options);
return context;
}
}
To read the connection string from your appsettings config file you could do the following:
public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory
{
public ActivitiesDbContext CreateDbContext(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
.AddJsonFile("appsettings.Development.json", optional: false);
var config = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder()
.UseSqlServer(config.GetConnectionString("DefaultConnection"));
return new ActivitiesDbContext(optionsBuilder.Options);
}
}
NOTE: in the above code factory will use connection string value defined in appsettings.Development.json file. And connection name is DefaultConnection. In other words, this disign time factory is used for the Code First commands like Add-Migration, Update-Database, etc...) and will apply them to the database connection configured for Development environment.