asp.net web api2 - Entity Framework DbContext Object Disposal -


environment : ef 6.0, sql 2012, webapi 2

approach 1:using block

  using(myentities test= new myentities ())               {                      var waredata = test.waretypes.tolist(); //here actual dbconnect , data fetching happens, hope m correct                      return request.createresponse<ienumerable<waretype>>(httpstatuscode.ok, waredata);                 } 

these scenario applicable irrespective of status of proxycreation flag

it fails if return

  • entire data model such
  • return entire data model , explicitly specifying entities needs included
  • use dto , return without explicit list conversion

it work 1 , if use

  • dto .. convert list , return it.

approach 2: without using block

 myentities test= new myentities ();            var waredata = test.waretypes; //here actual dbconnect , data fetching happens, hope m correct            return request.createresponse<ienumerable<waretype>>(httpstatuscode.ok, waredata); 

with approach, result vary , without proxycreation flag.. thing here object disposal error message not occuring.

question: setup:

  • proxy creation enabled
  • code enclosed in "using" block approach 1
  • returning entire data model , explictly specifiying entity needs included
  • converts data list within using block(to ensure db operation done)

expected result: self referencing loop(which m getting if dont enclose code in approach 2) actual output: dbcontext object disposed along self referencing loop error

interested in knowing explanation behaviour....

p.s : have working solution .... question more in terms of understanding concept..


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -