judehall
judehall

Reputation: 943

Is Database Context shared between sessions in Entity Framework?

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.

Upvotes: 1

Views: 394

Answers (1)

rudolfdobias
rudolfdobias

Reputation: 1958

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.

Upvotes: 1

Related Questions