Is there way that dependency injection can be configured/bootstrapped when using Entity Framework\'s migration commands?
Entity Framework Core supports dependency in
In .NET Core since version 2.1 should be used IDesignTimeDbContextFactory
because IDbContextFactory
is obsolete.
public class FooDbContextFactory : IDesignTimeDbContextFactory
{
public FooDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder();
var connectionString = configuration.GetConnectionString("ConnectionStringName");
builder.UseSqlServer(connectionString);
return new FooDbContext(builder.Options);
}
}