Answered by:
What if I don't use "using" for my context

Question
-
User283528319 posted
Hi all,
I simply get my data from context with the technique below
private readonly EvrakaContext _context; public NumuneKabulController(EvrakaContext context) { _context = context; } public JsonResult NumuneSahibiBul(string ArananNumuneSahibi) { IQueryable<NumuneSahipleri> bulunanNumuneSahipleri = _context.NumuneSahipleri.Where(e => e.Adisoyadi.Contains(ArananNumuneSahibi)).Include(e => e.Evraklar).OrderByDescending(e => e.EvraklarId); return Json(bulunanNumuneSahipleri); }
however, as you see my context is not IDisposable and this make me concern about my server's RAM.
What do you think?
Saturday, February 2, 2019 10:53 AM
Answers
-
User475983607 posted
Hi all,
I simply get my data from context with the technique below
private readonly EvrakaContext _context; public NumuneKabulController(EvrakaContext context) { _context = context; } public JsonResult NumuneSahibiBul(string ArananNumuneSahibi) { IQueryable<NumuneSahipleri> bulunanNumuneSahipleri = _context.NumuneSahipleri.Where(e => e.Adisoyadi.Contains(ArananNumuneSahibi)).Include(e => e.Evraklar).OrderByDescending(e => e.EvraklarId); return Json(bulunanNumuneSahipleri); }
however, as you see my context is not IDisposable and this make me concern about my server's RAM.
What do you think?
The construct shown in the snippet above is using constructor injection. I assume this is an ASP.NET Core application. The DbContext life time is determined by configuration set in ConfigurationServices. The default is "scoped" and which looks similar to the following.
var connection = @"Server=SqlServer;Database=AdventureWorksLT2008R2;Trusted_Connection=True;"; services.AddDbContext<AdventureWorksLT2008R2Context>(options => options.UseSqlServer(connection));
Scoped services are scoped to the request. You get one DbContext for the duration of the a request.
Don't use "using" when implementing DI. In the case of a scoped service, you can either end up disposing the context prematurely or creating a new context where the entities are in different states.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, February 2, 2019 2:22 PM
All replies
-
User475983607 posted
Hi all,
I simply get my data from context with the technique below
private readonly EvrakaContext _context; public NumuneKabulController(EvrakaContext context) { _context = context; } public JsonResult NumuneSahibiBul(string ArananNumuneSahibi) { IQueryable<NumuneSahipleri> bulunanNumuneSahipleri = _context.NumuneSahipleri.Where(e => e.Adisoyadi.Contains(ArananNumuneSahibi)).Include(e => e.Evraklar).OrderByDescending(e => e.EvraklarId); return Json(bulunanNumuneSahipleri); }
however, as you see my context is not IDisposable and this make me concern about my server's RAM.
What do you think?
The construct shown in the snippet above is using constructor injection. I assume this is an ASP.NET Core application. The DbContext life time is determined by configuration set in ConfigurationServices. The default is "scoped" and which looks similar to the following.
var connection = @"Server=SqlServer;Database=AdventureWorksLT2008R2;Trusted_Connection=True;"; services.AddDbContext<AdventureWorksLT2008R2Context>(options => options.UseSqlServer(connection));
Scoped services are scoped to the request. You get one DbContext for the duration of the a request.
Don't use "using" when implementing DI. In the case of a scoped service, you can either end up disposing the context prematurely or creating a new context where the entities are in different states.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, February 2, 2019 2:22 PM -
User283528319 posted
thanks
Saturday, February 2, 2019 2:38 PM