I'm setting up my first .NET 6 MVC site, and am falling at the first hurdle.
I have 3 projects in the solution. Domain: contains the Entities Infrastructure: has the applications DB context WebUi: contains the web interface
adding migrations and updating the database works fine, but when I try to scaffold a controller, it errors with:
There was an error running the selected code generator:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions’ While attempting to activate
Then names the DbContext in other class library project.
I've found others that have had similar issues a few months back, but no-one had any solutions other then bringing everything into the same project.
Has anyone encountered this and found a solution?
Finally found something that worked for me: You can create a dbcontext factory class in same folder with your ApplicationDbContext class. This factory class creates ApplicationDbContext at design time and scaffolding runs correctly
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=EcommerceDb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new ApplicationDbContext(optionsBuilder.Options);
}
}
Credit goes to : https://github.com/dotnet/Scaffolding/issues/1765#issuecomment-1058674843
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With