1

In Startup.cs it's possible to control dependency injection lifecycle using transients and singletons. However it's unclear how the lifecycle works when using .AddDBContext like so services.AddDbContext<DatabaseContext>(...);

Each controller uses this dependency by initialising it only once in the constructor and is reused throughout by the controller functions.

Is the context initialised for each request or is there a possibility this context being shared between user sessions resulting in bad state?

Note: duplicate question does not address if context is being shared between user sessions.

Vladel
  • 230
  • 1
  • 4
  • 16
judehall
  • 884
  • 12
  • 27
  • 1
    Possible duplicate of [Entity Framework Core service default lifetime](https://stackoverflow.com/questions/37507691/entity-framework-core-service-default-lifetime) – Simply Ged Dec 04 '18 at 01:20

1 Answers1

1

services.AddDbContext<>(...); registers your DbContext with Scoped lifetime. That means a new instance is created for every single request. No need to worry it would be shared with other connections.

rudolfdobias
  • 1,778
  • 3
  • 17
  • 40