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, Migrations.Configuration>());
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)
-
0
Hi @rogerscorporation,
Could you check this class?
https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.EntityFrameworkCore/EntityFrameworkCore/DatabaseCheckHelper.cs
-
0
Hi @rogerscorporation,
Could you check this class?
https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.EntityFrameworkCore/EntityFrameworkCore/DatabaseCheckHelper.cs
Hi @m.aliozkaya,
Could you please provide an example for this class? I still have no idea for configuring automatic update-database when application running in V14.0.0. It would be better if you have an example with some codes.
Thank you.
-
0
Hi @m.aliozkaya,
I added dbContext refer to this article: https://jasonwatmore.com/post/2019/12/27/aspnet-core-automatic-ef-core-migrations-to-sql-database-on-startup
However, still got error when starting application:
Here are the codes:
public IServiceProvider ConfigureServices(IServiceCollection services) { // DbContext services.AddDbContext<TaxonomyDbContext>(options => options.UseSqlServer( _appConfiguration.GetConnectionString("Default"))); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, TaxonomyDbContext dbContext) { // migrate any database changes on startup (includes initial db creation) dbContext.Database.Migrate(); }
-
0
Hi @rogerscorporation
Now, it is suggested to use Migrator project on your solution to update database. You can run this executable on your database before you publish the new version of your app.