Must I use "Using" block in Entity Framework 6 for DbContext

c# dbcontext entity-framework entity-framework-6

Question

Not about when/why to use it, etc., but rather on "Using" C# in general.

The concern is if the DBContext object will automatically close the connection so that I won't need to utiliseusing There is no doubt that it must be made to be disposed of, therefore Delete the duplicate entry

using (DBContext db = new DBContext())
{
    var Order =  db.Order.First(r => r.OrderID == 6);
    Order.Type = 6;
    db.SaveChanges();
}

or not using

DBContext db = new DBContext();
var Order =  db.Order.First(r => r.OrderID == 6);
Order.Type = 6;
db.SaveChanges();

Since I can see in this supplyusing is not required, and it would be best to avoid using it.

Entity Framework will it eliminate the connection for me?

1
3
7/10/2018 10:07:55 AM

Popular Answer

The context's lifespan starts when the instance is created and ends when it is either deleted or collected from the trash. If you wish to dispose of all the resources that the context controls at the conclusion of the block, use utilising.

The compiler automatically builds a try/finally block and runs dispose in the finally block whenever you use using.

1
11/11/2019 10:19:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow