entity framework - The ConnectionString property has not been initialized -


i using entity framework make 2 queries, 1 after another, first 1 works fine, second 1 returns me: connectionstring property has not been initialized.

if change order of methods, same thing happen, first 1 works fine, second 1 throws exception.

the code use in page follows:

>  var count = requestbasebl.getgenericresultscount(query.querysql);     >  >                     var datatable = requestbasebl.getgenericresults(query.querysql, 0); 

and in dal

public int  getgenericresultscount(string strsql)             {                 using (var connection = (sqlconnection)_context.database.connection)                 {                     var adapter = new sqldataadapter(strsql, connection);                     var results = new dataset();                     adapter.fill(results, "results");                     return results.tables["results"].rows.count;                 }             }           public datatable getgenericresults(string strsql, int pageindex)             {                 stringbuilder sb = new stringbuilder();                 sb.append("with mypageddata ( ");                 int indexfrom = strsql.indexof("from");                 sb.append(strsql.substring(0, indexfrom));                 sb.append(", ");                 sb.append("row_number() over(order requestbaseid desc) rownum ");                 sb.append(strsql.substring(indexfrom));                 sb.append(") ");                 sb.append("select * mypageddata rownum between @startindex , @startindex + 10");                   using(var connection = (sqlconnection)_context.database.connection)                 {                     var adapter = new sqldataadapter(sb.tostring(), connection);                     adapter.selectcommand.parameters.add("@startindex", sqldbtype.int).value = pageindex;                     var results = new dataset();                     adapter.fill(results, "results");                     return results.tables["results"];                 }             } 

in general, using construct should used when create object assigned variable, either new operator or though factory method. connection property of database in dbcontext creates connection if not exist already; in other cases, property returns existing one.

in code, first invocation gets "live" connection, operates on it, , closes through call of dispose implicitly performed using. @ point, database of dbcontext has reference released object. second invocation picks released object, , tries use it, triggering error.

to fix this, replace using assignments:

var connection = (sqlconnection)_context.database.connection; 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -