Skip to content

Instantly share code, notes, and snippets.

@tonysneed
Last active September 4, 2023 22:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysneed/a2981790917919d1c7e6bf2213474362 to your computer and use it in GitHub Desktop.
Save tonysneed/a2981790917919d1c7e6bf2213474362 to your computer and use it in GitHub Desktop.
ProductsDbContextFactory3
public class ProductsDbContextFactory : IDesignTimeDbContextFactory<ProductsDbContext>
{
public ProductsDbContext CreateDbContext(string[] args)
{
// Get environment
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
// Build config
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../EfDesignDemo.Web"))
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables()
.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);
}
}
@desuitech
Copy link

I have this error "rror Number:207,State:1,Class:16
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Invalid column name 'AssociatedBranch'.
Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728"
Kindly assist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment