Skip to content

Instantly share code, notes, and snippets.

@tonysneed
Last active December 20, 2018 20:59
Show Gist options
  • Save tonysneed/933c0e5a9654181832ae4b7469d5312a to your computer and use it in GitHub Desktop.
Save tonysneed/933c0e5a9654181832ae4b7469d5312a to your computer and use it in GitHub Desktop.
ProductsDbContextFactory2
public class ProductsDbContextFactory : IDesignTimeDbContextFactory<ProductsDbContext>
{
public ProductsDbContext CreateDbContext(string[] args)
{
// Build config
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../EfDesignDemo"))
.AddJsonFile("appsettings.json")
.Build();
// Get connection string
var optionsBuilder = new DbContextOptionsBuilder<ProductsDbContext>();
var connectionString = config.GetConnectionString(nameof(ProductsDbContext));
optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("EfDesignDemo.EF.Design"));
return new ProductsDbContext(optionsBuilder.Options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment