Answered by:
Querying objects in memory

Question
-
Still trying to get my head around EF so I apologize if my question is basic.
Say I had the following code:
using (ContosoEntities context = new ContosoEntities()) { var results = context.Products.Execute(...); Product p = new Product() { Name = "Foo" }; context.AddToProducts(p); var getP = context.Products.Where("it.Name = 'Foo'").Execute(...); }
Will getP have the new product "Foo"? Did the query to assign getP have to make a trip to the database?
What I am trying to understand is if when I query for entities, if I need to build my own cache so that I can minimize trips to the database.
Thanks!
Friday, July 16, 2010 12:39 AM
Answers
-
"TonyHernandez" wrote in message news:90aed174-05d1-4a6e-8262-e3c4388e51db...
Still trying to get my head around EF so I apologize if my question is basic.
Say I had the following code:
using (ContosoEntities context = new ContosoEntities()) { var results = context.Products.Execute(...); Product p = new Product() { Name = "Foo" }; context.AddToProducts(p); var getP = context.Products.Where("it.Name = 'Foo'").Execute(...); }
Will getP have the new product "Foo"? Did the query to assign getP have to make a trip to the database?
My response---------------------------------------------------------------------------------------------------
I suspect that it went back twice to the database to get the object, which can be checked by using VS debugger and SQL Server Profiler to see if it executed T-SQL against the database table as you single step and execute the query.
------------------------------------------------------------------------------------------------------------------
What I am trying to understand is if when I query for entities, if I need to build my own cache so that I can minimize trips to the database.
my response -----------------------------------------------------------------------------------------------------------
I think based on the type of queries you used above, it went back to the database to read it and put it back into cache, as opposed to using GetObjectByKey, that will search cache first before it goes back to the database and put the object back into cache if it's not in cache.
You should also be aware that even if you query and get the objects in memory, EF will still go back to the database on a foreach loop of reading the results. So, you really have to be aware of what is happening when you do things, which SQL Server Profiler will reveal to you.
- Proposed as answer by liurong luo Wednesday, July 21, 2010 8:04 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Friday, July 23, 2010 6:06 AM
Friday, July 16, 2010 10:13 AM -
"TonyHernandez" wrote in message news:dfd71a5d-82de-4a90-8ad7-15b562a5b268...
Thanks for the response.
How do I know what the entity key is I'm looking for?
my response ------------------------------------------------------------
And EF entity is derived from a database table that has a primary key and relationships to other tables based on foreign key constrains. So, there are going to be properties in the entity that reflect the keys from the underlying database table.
However, when using a GetObjectByKey, it can be use to query any property on the entity to retrieve the entity. They key here is GetObjectByKey is going to query cache first while Linq-2-Entity is going back to the database.
- Marked as answer by Michael Sun [MSFT]Microsoft employee Friday, July 23, 2010 6:06 AM
Tuesday, July 20, 2010 5:19 AM
All replies
-
"TonyHernandez" wrote in message news:90aed174-05d1-4a6e-8262-e3c4388e51db...
Still trying to get my head around EF so I apologize if my question is basic.
Say I had the following code:
using (ContosoEntities context = new ContosoEntities()) { var results = context.Products.Execute(...); Product p = new Product() { Name = "Foo" }; context.AddToProducts(p); var getP = context.Products.Where("it.Name = 'Foo'").Execute(...); }
Will getP have the new product "Foo"? Did the query to assign getP have to make a trip to the database?
My response---------------------------------------------------------------------------------------------------
I suspect that it went back twice to the database to get the object, which can be checked by using VS debugger and SQL Server Profiler to see if it executed T-SQL against the database table as you single step and execute the query.
------------------------------------------------------------------------------------------------------------------
What I am trying to understand is if when I query for entities, if I need to build my own cache so that I can minimize trips to the database.
my response -----------------------------------------------------------------------------------------------------------
I think based on the type of queries you used above, it went back to the database to read it and put it back into cache, as opposed to using GetObjectByKey, that will search cache first before it goes back to the database and put the object back into cache if it's not in cache.
You should also be aware that even if you query and get the objects in memory, EF will still go back to the database on a foreach loop of reading the results. So, you really have to be aware of what is happening when you do things, which SQL Server Profiler will reveal to you.
- Proposed as answer by liurong luo Wednesday, July 21, 2010 8:04 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Friday, July 23, 2010 6:06 AM
Friday, July 16, 2010 10:13 AM -
Thanks for the response.
How do I know what the entity key is I'm looking for?
Monday, July 19, 2010 8:27 PM -
"TonyHernandez" wrote in message news:dfd71a5d-82de-4a90-8ad7-15b562a5b268...
Thanks for the response.
How do I know what the entity key is I'm looking for?
my response ------------------------------------------------------------
And EF entity is derived from a database table that has a primary key and relationships to other tables based on foreign key constrains. So, there are going to be properties in the entity that reflect the keys from the underlying database table.
However, when using a GetObjectByKey, it can be use to query any property on the entity to retrieve the entity. They key here is GetObjectByKey is going to query cache first while Linq-2-Entity is going back to the database.
- Marked as answer by Michael Sun [MSFT]Microsoft employee Friday, July 23, 2010 6:06 AM
Tuesday, July 20, 2010 5:19 AM