Base solution for your next web application
Open Closed

GetDbContext() is null #12317


User avatar
0
Jorahealth created

Product Version: V13.20 Product Type: Angular Product Framework: .Net Core6

Hi, We have migrated code from 9.2.0 to 13.2.0, the application is running, we can create tenant, and login as a tenant. When we try to create user under host or tenant, we get an internal error with **GetDbContext() **is null. Could you kindly guide to resolve the issue please.

Many Thanks


10 Answer(s)
  • User Avatar
    0
    Jorahealth created

    Hi Support Team,

    Additional Information: Upon further investigation, I found that the DbContext.cs file in the new codebase version (13.2.0) contains 134 lines of code. However, during migration, we pulled our old code with the DbContext.cs file from our old codebase, which has around 1400 lines of code, including all the entities.

    Could this discrepancy in the DbContext.cs file be causing the GetDbContext() null issue mentioned earlier? If yes, could you advise on the best approach to resolve this? Should we retain the old code, or is it necessary to adapt to the new version's structure?

    Many Thanks.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Jorahealth

    In order to provide better assistance for this situation, can you share your project with us at [email protected]?

  • User Avatar
    0
    Jorahealth created

    Hi,

    I have sent an email to [email protected] with the project files attached, as requested. Please let me know if you require any additional details or further clarification.

    Looking forward to your response.

    Many Thanks.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Jorahealth

    The issue here is that the DbContext instance is automatically managed during the UnitOfWork process. If a UnitOfWork process has not started, the GetDbContext() call will return null. You can directly access the active DbContext using the IUnitOfWorkManager.

    public class AdoRepository : DecisivelyRepositoryBase<User, long>, IAdoRepository
    {
        private readonly IActiveTransactionProvider _transactionProvider;
        private readonly DecisivelyDbContext _dbContext;
    
        
        public AdoRepository(
            IDbContextProvider<DecisivelyDbContext> dbContextProvider, 
            IActiveTransactionProvider transactionProvider, 
            IUnitOfWorkManager unitOfWorkManager)
            : base(dbContextProvider)
        {
            _transactionProvider = transactionProvider;
            _dbContext = unitOfWorkManager.Current?.GetDbContext<DecisivelyDbContext>();
        }
    
        // ...
    }
    
  • User Avatar
    0
    Jorahealth created

    Dear Support Team,

    Thank you for your detailed response and suggestion regarding the UnitOfWork process. The issue has been resolved successfully based on your guidance, and everything is now working as expected.

    I have one follow-up question:

    Is this solution a custom fix or workaround specific to our scenario? If not, could you clarify why this approach or guidance is not included in the base version by default? Your insight would be greatly appreciated.

    Many Thanks.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Jorahealth

    We have made a suggestion like this because you want to inject dbContext with DI. Here you can also perform your operations by using the GetDbContext method coming from YourProjectNameRepositoryBase.

    [UnitOfWork]
    public class AdoRepository : DecisivelyRepositoryBase<User, long>, IAdoRepository
    {
        private readonly IActiveTransactionProvider _transactionProvider;
    
        public AdoRepository(IDbContextProvider<DecisivelyDbContext> dbContextProvider,
            IActiveTransactionProvider transactionProvider)
            : base(dbContextProvider)
        {
            _transactionProvider = transactionProvider;
        }
    
    
        private SqlCommand CreateCommand(string commandText, CommandType commandType, params SqlParameter[] parameters)
        {
            var command = GetDbContext().Database.GetDbConnection().CreateCommand();
            //...
        }
    
        private async Task EnsureConnectionOpenAsync()
        {
            var connection = GetDbContext().Database.GetDbConnection();
    
            //...
        }
    }
    
  • User Avatar
    0
    Jorahealth created

    Hi Support Team,

    Thank you so much for your response.

    We recently migrated our codebase from version 9.2.0 to 13.2.1, and we are now attempting to configure our old database with the new migrated code. However, we are encountering issues when running the migrator.

    The primary challenge seems to be related to differences in the database schema:

    The new version introduces additional columns, such as " RecoveryCode ", which are not present in the old database schema. This discrepancy appears to be causing errors during the migration process. Could you kindly advise on the best approach to use our old database with the new migrated code? Should we manually update the old database schema to align with the new codebase, or is there a recommended process for handling this situation?

    Looking forward to your guidance.

    Many thanks.

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Jorahealth,

    You can incorporate new changes into your project by adding a migration. If you prefer, you can also delete all existing migrations and create a new one to manage this process. https://www.learnentityframeworkcore.com/migrations/add-migration

  • User Avatar
    0
    Jorahealth created

    Dear Support Team,

    First of all, I would like to thank you for your prompt and helpful responses to my previous queries. Your guidance has been invaluable, and I truly appreciate the support you’ve provided so far.

    I’m reaching out again regarding another issue with the permissions functionality in our application.

    Issue Details: In the new Angular base code (v18), the functionality for adding permissions to users is working perfectly. However, in the migrated Angular code (v10 to v18), the same functionality does not work. I have not modified any of the related components or shared files during the migration and copied all relevant files from the new base code. I also compared the connected components in both versions, and there are no visible code differences.

    Could you kindly suggest what might be causing this discrepancy? Are there any hidden dependencies or configurations that might affect this functionality in the migrated code?

    I greatly value your assistance and look forward to your guidance in resolving this issue.

    Many thanks.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Jorahealth

    Can you elaborate on a few points so we can better understand your issue? Are you having trouble with the permission tree, which lists permissions on the user interface and makes them selectable? Apart from that, can you share your primeng package version with us?