Open Closed

Recommended way to inject DBContext into AppService #1252


User avatar
0
joe@tronactive.com 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;


2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    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();
    
  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

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 18, 2025, 10:42