ObjectContext adding and requery

  • Thread starter Thread starter eager2008
  • Start date Start date
E

eager2008

Using Entity Framework (VS2008 SP1 and .NET Framework 3.5 SP1).
Consider the following code:
(context is an ObjectContext given by EDM code generation)

var qBefore =
from myEntity in context.MyEntities
select myEntity ;
int beforeCount = qBefore.Count();

MyEntity newMyEntity = MyEntity.CreateMyEntity(<neccessary parameters>);
context.AddToMyEntities(newMyEntity);

var qAfter =
from myEntity in context.MyEntities
select myEntity ;
int afterCount = qAfter.Count();

Why is afterCount same as beforeCount (I know I have not performed any
SaveChanges, but I thought the query is working against the cache) ?
Would a transaction be helpful ?
 
Hello Eager,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Based on my understanding, your question is why the second query returns
the same count as the first does after adding a record manually. If I have
misunderstood the question, please feel free to let me know.

I did the following test on my side. I used the following two select
statements and set a breakpoint before the second one. When the first
select statement returned, I opened my SQL Server Management and inserted
one row. Afterwards, the second select statement returned one more record.
This proves that the select statement does not use the cache, but always
queries from the database to returns the result. So, if we want the select
statement return the updated data source, we have to call the
context.SaveChanges as you found.

var qBefore =
from myEntity in context.MyEntities
select myEntity ;
int beforeCount = qBefore.Count();

var qAfter =
from myEntity in context.MyEntities
select myEntity ;
int afterCount = qAfter.Count();

In fact, the Linq to entity uses another way to cache the data. When we
have modified the data, the corresponding rows will be set with a state
mark. We can use ObjectStateManager to access these modified values. Code
looks like:

using (NorthwindEntities context = new NorthwindEntities())
{
var qBefore = from myEntity in context.Products
select myEntity;
int beforeCount = qBefore.Count();

Products newMyEntity = Products.CreateProducts(true, 81, "Test
ProductName");
context.AddToProducts(newMyEntity);

int i =
context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count();
}

The value i will be 1 which represents the new added record. If you have
any future questions or concerns on this, please feel free to post, and I
will do my best to help.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top