How to resolve context of current EF7 database in ASP NET 5 from controller?

I want to get one context for each request in an ASP NET 5 / EF 7 application, so that it can be used in some methods (and not in the controller).

Unfortunately, I did not find an answer in the ASP.NET vNext template documentation and aspnet / MusicStore examples

+5
source share
1 answer

You can use some methods to achieve this.

Using the .AddDbContext<ApplicationDbContext>(); method .AddDbContext<ApplicationDbContext>(); to register ApplicationDbContext in the Injection Dependency system (in the ConfigureServices() method) leads to the fact that it is registered as a dependency on Scoped (or in other words β€œper request”). Thus, you only need to get it from the Injection Dependency system.

  • Add your dbContext as a parameter to the constructor method of your class (in which you will use dbContext). Then you should get this class using the Injection Dependency system, for example, adding it as a parameter of the controller constructor.

     public class HabitsController : Controller { public HabitsController(HabitService habitService) { } } public class HabitService { private GetHabitsContext _dbContext; public HabitService(GetHabitsContext dbContext) { _dbContext = dbContext; } } 
  • But if you do not want to use the constructor injection to get the context, you can get the necessary dependencies using the GetService() method (but you need an instance of ServiceProvider for this, in the example below, I get it through the constructor injection too).

     using Microsoft.Framework.DependencyInjection; // for beta 6 and below using Microsoft.Extensions.DependencyInjection; // for beta 7 and above public class HabitService { private IServiceProvider _serviceProvider; public HabitService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public GetHabit() { var dbcontext = _serviceProvider.GetService<ApplicationDbContext>(); } } 
  • In the first method, we can get the HabitService method through GetService() (not through constructor injection).

     using Microsoft.Framework.DependencyInjection; // for beta 6 and below using Microsoft.Extensions.DependencyInjection; // for beta 7 and above public class HabitsController : Controller { public HabitsController(IServiceProvider serviceProvider) { var habitService= serviceProvider.GetService<HabitService>(); } } public class HabitService { private GetHabitsContext _dbContext; public HabitService(GetHabitsContext dbContext) { _dbContext = dbContext; } } 

Thanks to Zeng for the comment:

It should be noted that it is pretty bad practice to insert a container into your objects. The container should refer only from the root of the composition and a certain type of factories (which are implemented at the application level, and not at the domain / business level)


The dbContext in the HabitsController and the _dbContext in the HabitService are different contexts!

I checked this is the same context.

+6
source

Source: https://habr.com/ru/post/1233340/


All Articles