-2

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
  • 16,532
  • 28
  • 100
  • 184

1 Answers1

0

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;
}

}

AmitKumar
  • 638
  • 10
  • 12