0

i'm using entityframework 5 and i've noticed that if i load a object from my db using linq my result object is stored on dbcontext, but if i whant to load the same object using the ExecuteStoreQuery, the result is not stored on db context.

is this the expecte behavior? or what could i do to maintain the result of secon way query con dbcontext?

1st way (LINQ):

var obj = dbcontext.table.where(o=> o.id == queryId).FirstOrDefault();

2nd way (ExecuteStoreQuery):

var obj = dbcontext.executestorequery("select * from...").FirstOrDefault();
Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63
  • Step 1: [Look for overloads of the method you are using](http://msdn.microsoft.com/en-us/library/ee835300.aspx) – AakashM Dec 12 '12 at 12:34

1 Answers1

1

Database in DbContext API doesn't have ExecuteStoreQuery - that is a method from ObjectContext. DbContext API uses just SqlQuery method.

If you want to get the same behavior with native SQL as you get with Linq query you must use:

var obj = dbcontext.table.SqlQuery("select * from ... ");

The difference between calling SqlQuery directly on DbContext instance or on DbSet<> instance is that in the first case result is not attached and tracked by the context but in the second case it is.

If you are using ObjectContext API you need to use overloaded version of ExecuteStoreQuery and pass the name of entity set as a second parameter otherwise results are not tracked by the context.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670