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, 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)
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    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

  • User Avatar
    0
    rogerscorporation created

    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.

  • User Avatar
    0
    rogerscorporation created

    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();
    }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    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.