c# - EF 5 Connection Management -
is possible ef5 not close connections when using dbcontext like
public void updatecategory(models.category catdata) { if (catdata == null) return; using (var cntx = new datacontext()) { //in line below connection opened close???? var cat = cntx.set<category>().firstordefault(c => c.categoryid == catdata.categoryid); if (cat == null) return; if (!cat.lastchanged.matches(catdata.lastchanged)) throw new concurrencyexception(cat.gettype().tostring()); cat.categoryname = catdata.categoryname; cntx.dbcontext.entry<category>(cat).state = system.data.entitystate.modified; //after next line have 2 connections opened? or connection opened first query closed already? cntx.savechanges(); catdata.lastchanged = cat.lastchanged; } }
are there scenerios\bugs same context creates\opens 2 connections, leaving 1 open?
if @ msdn here: http://msdn.microsoft.com/library/vstudio/bb896325
the entity framework opens connections when required, example execute query or call savechanges, , closes connection when operation complete.
when query method called, connection opened, , remains open until objectresult has been consumed or disposed.
public void updatecategory(models.category catdata) { if (catdata == null) return; using (var cntx = new datacontext()) { //in line below connection opened //immediately close? => yes! var cat = cntx.set<category>() .firstordefault(c => c.categoryid == catdata.categoryid); if (cat == null) return; if (!cat.lastchanged.matches(catdata.lastchanged)) throw new concurrencyexception(cat.gettype().tostring()); cat.categoryname = catdata.categoryname; cntx.dbcontext.entry<category>(cat).state = system.data.entitystate.modified; //after next line have 2 connections opened? => no cntx.savechanges(); catdata.lastchanged = cat.lastchanged; } }
are there scenerios same context creates\opens 2 connections, leaving 1 open?
yes, when using transaction dbcontext open second connection have here: http://www.digitallycreated.net/blog/48/entity-framework-transactionscope-and-msdtc
Comments
Post a Comment