Open Closed

DB migration issue #8756


User avatar
0
Nathan created

Hi support team,

Could you help me which part am I missing when I upgrade from .net version 8 to 9? After I upgrade to .net version 9.0 I met this issue when running migration cmd like: "add-migration... or update-database" Here is the err:

Could you help to give me the solution to resolve it? Thank you


9 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, it seems the appsettings.json path is wrong:

    Can you please share your *.DbContextFactory class? (and do you use MacOS or Windows?)

  • User Avatar
    0
    Nathan created

    Hi EngincanV, Here is DbContextFactory file. Could you help me to check?

    using System;
    using System.IO;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Design;
    using Microsoft.Extensions.Configuration;
    
    namespace Eduverse.EntityFrameworkCore;
    
    /* This class is needed for EF Core console commands
     * (like Add-Migration and Update-Database commands) */
    public abstract class EduverseDbContextFactoryBase<TDbContext> : IDesignTimeDbContextFactory<TDbContext>
        where TDbContext : DbContext
    {
        public TDbContext CreateDbContext(string[] args)
        {
            // https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            
            var configuration = BuildConfiguration();
    
            EduverseEfCoreEntityExtensionMappings.Configure();
    
            var builder = new DbContextOptionsBuilder<TDbContext>()
                .UseNpgsql(configuration.GetConnectionString("Default"));
    
            return CreateDbContext(builder.Options);
        }
    
        protected abstract TDbContext CreateDbContext(DbContextOptions<TDbContext> dbContextOptions);
    
        protected IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Eduverse.DbMigrator/"))
                .AddJsonFile("appsettings.json", optional: false);
    
            return builder.Build();
        }
    }
    
    
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi EngincanV, Here is DbContextFactory file. Could you help me to check?

    using System; 
    using System.IO; 
    using Microsoft.EntityFrameworkCore; 
    using Microsoft.EntityFrameworkCore.Design; 
    using Microsoft.Extensions.Configuration; 
     
    namespace Eduverse.EntityFrameworkCore; 
     
    /* This class is needed for EF Core console commands 
     * (like Add-Migration and Update-Database commands) */ 
    public abstract class EduverseDbContextFactoryBase<TDbContext> : IDesignTimeDbContextFactory<TDbContext> 
        where TDbContext : DbContext 
    { 
        public TDbContext CreateDbContext(string[] args) 
        { 
            // https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic 
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 
             
            var configuration = BuildConfiguration(); 
     
            EduverseEfCoreEntityExtensionMappings.Configure(); 
     
            var builder = new DbContextOptionsBuilder<TDbContext>() 
                .UseNpgsql(configuration.GetConnectionString("Default")); 
     
            return CreateDbContext(builder.Options); 
        } 
     
        protected abstract TDbContext CreateDbContext(DbContextOptions<TDbContext> dbContextOptions); 
     
        protected IConfigurationRoot BuildConfiguration() 
        { 
            var builder = new ConfigurationBuilder() 
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Eduverse.DbMigrator/")) 
                .AddJsonFile("appsettings.json", optional: false); 
     
            return builder.Build(); 
        } 
    } 
     
    

    Hi again, can you make the following change and let me know if it fix your problem:

    - .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Eduverse.DbMigrator/"))
    + .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), @"..\Eduverse.DbMigrator\"))
    
  • User Avatar
    0
    Nathan created

    Hi sir, it's still err like that. I don't know why if I using version 8 then it's ok. Could you give me another solution?

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi sir, it's still err like that. I don't know why if I using version 8 then it's ok. Could you give me another solution?

    Can you share your application via email to support@abp.io (with ticket number), so I can quickly check and fix the problem?

    Regards.

  • User Avatar
    0
    Nathan created

    Hi sir, Thank you for that but I can not share our application. For your information: I am using abp framework with .net version 9.0. we have 2 context 1 is XDbcontext 2 is XTenantDbContext. But I just met issue with XDbcontext and other I able to run migration. Eventhough, the XtenantDbContext using with the same DbcontextFactoryBase.

  • User Avatar
    0
    Anjali_Musmade created
    Support Team Member

    Hello

    Can you please check this document Create a Second DbContext to create a second migration if it will helps you.

    Thank you.

  • User Avatar
    0
    Nathan created

    Hi, we already implemented 2 Dbcontext and it work fine. But when we upgrade to .net version 9.0 then it happen.

  • User Avatar
    0
    Nathan created

    Hi, I already resolved this issue. The cause come from when I inject the Logger on DbContext then it's not able to create instance for that. Thank you all for your supporting me

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 20, 2025, 18:00