Why Microsoft.EntityFrameworkCore.Design is not included in WebUI? #690
-
I'm using your awesome template, but I cannot perform migrations as per sample code on the repo readme. The error is: Should I go ahead by adding the package? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
(Not the owner of this repo) What I do instead is implement a custom IDesignTimeDbContextFactory in the Infrastructure project, and omit the Doing this will eliminate the need to reference Microsoft.EntityFrameworkCore.Design in your WebUI project. (PS if someone smarter than me sees pitfalls here - please lmk!) public class MyAwesomeDbContextFactory : IDesignTimeDbContextFactory<MyAwesomeDbContext>
{
public MyAwesomeDbContext CreateDbContext(string[] args)
{
// sample migration add command (run from src/Infrastructure): dotnet ef migrations add <MigrationName> -c MyAwesomeDbContext -o \Persistence\Migrations\ -- Development
var env = args[0];
var jsonFile = "";
if (env == "Production")
{
jsonFile = "appsettings.json";
}
else if (env == "Development")
{
jsonFile = "appsettings.Development.json";
}
else
{
throw new NotImplementedException("No appsettings.json for environment named " + env);
}
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
.AddJsonFile(jsonFile)
.AddEnvironmentVariables();
var optionsBuilder = new DbContextOptionsBuilder<MyAwesomeDbContext>();
var config = configBuilder.Build();
optionsBuilder
.UseMyAwesomeDbContext(config)
return new MyAwesomeDbContext(optionsBuilder.Options, config);
}
} |
Beta Was this translation helpful? Give feedback.
-
I have another trick. Just keep the appsettings.json inside the WebUI and use this code inside the Infrastructure,
|
Beta Was this translation helpful? Give feedback.
(Not the owner of this repo) What I do instead is implement a custom IDesignTimeDbContextFactory in the Infrastructure project, and omit the
--startup-project
argument. I have two separate codebases that this works really well for. Here's a sample (had to remove a few proprietary details, so there may be an error or two).Doing this will eliminate the need to reference Microsoft.EntityFrameworkCore.Design in your WebUI project.
(PS if someone smarter than me sees pitfalls here - please lmk!)