I have the following code, where I want to immediately dispose of ApplicationDbContext
instance:
using (var context = ApplicationDbContext.Create())
{
MyRepository myRespository = new MyRepository(context);
myRepository.ReadData();
}
If C# garbage collector wants to immediately dispose of ApplicationDbContext
, it would have to immediately dispose of MyRepository
as well, because it has a reference to ApplicationDbContext
? Is this actually happening?
Or do I need to write my code like below to ensure ApplicationDbContext
is Disposed, immediately?
using (var context = ApplicationDbContext.Create())
using (MyRepository myRespository = new MyRepository(context))
{
myRepository.ReadData();
}