Reputation: 1741
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TestContext>(options => options.UseSqlServer("dbconnection"));
}
private readonly TestContext _db;
public TestController(TestContext db)
{
_db = db;
}
public void Get()
{
DALUser dal = new DALUser(_db);
var list = dal.Get();
}
private readonly TestContext _db;
public DALUser(TestContext db)
{
_db = db;
}
public IQueryable<User> Get()
{
_db.Users.AsQueryable();
}
Will the dbcontext dispose and close after executing Get() function?
Upvotes: 0
Views: 61
Reputation: 1026
Default lifetime for DatabaseContext is scoped . So in your web application , DatabaseContext disposed after your http request finished ( TestController-Get ) .
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
[CanBeNull] Action<DbContextOptionsBuilder> optionsAction = null,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
Upvotes: 1