1

when i'm using entity framework does it create objects for all data in the database for example context.Customers.Load(); will it create 1000 customer objects and then 5000 order objects, and if so does it not use a lot of memory?

2 Answers 2

2

You should create the EF context from the moment you need it and dispose it from the moment you are done (unit of work, using statement). And of course, you should only query what you need - I doubt you need 5000 orders in one request.

And yes, EF creates entities for every query. Navigation properties are not loaded, you have to include these specifically or use lazy loading (but I would avoid this).

Sign up to request clarification or add additional context in comments.

4 Comments

first on msdn: "When working with Windows Presentation Foundation (WPF) or Windows Forms, use a context instance per form. This lets you use change-tracking functionality that context provides", so i'm using WPF, and whats if i need a list (DataGrid) from all customers or orders
Then make sure your question contains enough information, we cannot guess that. And it sounds fishy to me that your WPF application access a database directly. Even then, you don't need 5000 orders.
Hi its not the number 5000, what i want to know is again if EF creates objects in memory for every raw in the table i'm querying, and you didn't answer me whats if i need a list of all customers? thanks
Ef creates objects for every result row. If the query returns 5000 rows, it creates 5000 objects. Even if you need a list of all customers, you should still be using pagination to get smaller results
1

In addition to L-Three's response, using the using statement may not fit all circumstances. For example, with desktop applications, it makes more sense to keep the connection open instead of open and close connections all the time to query simple data. It is important to make sure you dispose of it when done though. A good place would be to listen for the Closing event on the window. When triggered, you could dispose of the DbContext.

If you don't reduce the scope of the query, eg: using a where clause, EF will load all the objects from the database when you try to access them. By doing var query = Db.Users; you aren't actually querying the database yet, you just created a query that will be sent when you try to access the data. If you did var query = Db.User.ToList();, then yes, the query would be executed and all the User objects will be queried, mapped, and returned. So if you have 1 million rows, EF will try to load it 1 million records. In most cases, there is no reason to return all object from the database, but there are always fringe cases.

The child relationship is a little different depending on how you have the context configured. By default, I believe, EF uses something called lazy-loading. Lazy-loading allows EF to override your properties with a special code that won't load the child objects until you try to access them. On the other side of this, you can do what is called Eager-loading. This allows you to include all the child objects in the query as to make fewer trips to the database. In most cases, eager-loading should be used since you should know what information your application will be displaying to the user.

If you only ever use lazy-loading you can get into some trouble because it just works and it can cause performance issues down the road by creating n+1 statements for something simple.

13 Comments

So if i want a list of customers in a list box and than when the user select a customer it shows the orders for that customer in a datagrid(for example QuickBooks have a list of all transactions) will EF create objects in memory for each order right?! so is it not a problem of memory usage?
Yes, EF will load all of the customers and will keep track of changes to it in memory. When you access the navigation properties of the customer object, it will then load those into memory and keep track of changes to those. It could become an issue of memory usage depending on how many entities are return and the object graphs that follow. There is also the issue of load time.
and when i select a second customer will the orders of the first customer be garbage collected?
No, EF tracks the state of all the objects that are loaded using it. In order to stop tracking it, the DbContext needs to be disposed of or the entity needs to be detached from it so EF knows not to track the entity anymore. Remember, garbage collection only works if nothing holds a reference to the object anymore or manually disposed of so the GC knows it can clean it up on the next execution cycle.
Since you are using the properties of the Orders object, yes, EF will load those lazily if you don't include it. You should include the Orders table with the customers if you need this data at the same time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.