Open Closed

Recommended way to inject DBContext into AppService #1252

[email protected] created

What is the recommended way to use a DBContext directly in a service? Is it something like this? var masterDbContext = (Integr8MasterDbContext) await _customerRepository.GetDbContextAsync();

Or directly inject the DBContext like this? private readonly Integr8MasterDbContext _masterDbContext;

1 Answer(s)
  • 0
    liangshiwei created
    Support Team


    You can't directly inject the DBContext. Because the context instance is controlled by the unit of work system.

    So, recommended way is :

    var masterDbContext = (Integr8MasterDbContext) await _customerRepository.GetDbContextAsync();