-1

I decided to use this AddDbContext method to add and setup my context for my Entity Framework Core project.

services.AddDbContext<ExampleContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ExampleConnection"))); 
// https://stackoverflow.com/a/51970589/196526

I suppose this AddDbContext allow us to add a global context and it is possible to retrieve it later when required in my controller or service class. How can I use it?

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

1 Answers1

2

Well, dotnet core has got dependency injection inbuilt now. The way you use it in your controllers, service or repository classes is as simple as through a constructor injection.

Example -

public class AccountRepository : IAccountRepository{
   private readonly DbContext _exampleContext;
 public AccountRepository(ExampleContext context){
   _exampleContext = context;
}

}

Amit Kumar
  • 813
  • 11
  • 13