On Startup.cs, the application establishes connection with the database.
services.AddDbContext<DataContext>(opt=>
opt.UseSqlLite(_config.GetCinnectionString(‘DefaultConnectionString’));
});
This opt object of the now established database connection is injected as options to the DataContext class-
public class DataContext:DbContext
{
public DataContext(DbContextOptions options):base(options)
}
And further on, whenever DataContext is referred to on different classes like
public class SomeMethod
{
private readonly DataContext _context;
SomeMethod(DataContext context)
{
_context = context:
}
}
The execution flow is - SomeMethod=>DataContext(which gets its options object from Startup) referring to the applications session with the database.
I hope we are not establishing a new connection with the database each time we are referring to DataContext.
Would love to read the answers. And please feel free to add details/intricacies to the flow as I haven’t been able to find many answers regarding this.
CodePudding user response:
Check here everything for EF Core
The default it's scoped.