Base solution for your next web application
Open Closed

How to configure automatic update-database when application running in V14.0.0 #12310


User avatar
0
rogerscorporation created

Hello,

We have been able to automatic update-database when application running in .NET 5 MVC & jQuery solution.
However we have no idea for .NET 9.0 solution in v14.0.0.

Here are codes for reference:

For .NET 5 MVC In Web\Global.asax.cs

// Configure automatic update-database when application running
Database.SetInitializer(new MigrateDatabaseToLatestVersion());
MasterDataToolDbContext dbContext = new MasterDataToolDbContext();
dbContext.Database.Initialize(true); // force the initialization

For .NET Core 6
You can call context.Database.Migrate()in your Startup.cs

eg:

using (var context = new MyContext(...))
{
    context.Database.Migrate();
}

e.g.:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, TaxonomyDbContext dbContext)
{
    // migrate any database changes on startup (includes initial db creation)
    dbContext.Database.Migrate();
}

We got error when running the application, it seems codes cannot work in .NET 9 solution.
ERROR:
'Could not resolve a service of type '.EntityFrameworkCore.TaxonomyDbContext' for the parameter 'dbContext' of method 'Configure' on type 'Rogers.Taxonomy.Web.Startup.Startup'.'

Could you please help troubleshoot this and give us some advice?

Thank you.


4 Answer(s)