Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 eager load single property of related entity

IN EF6, i have an entity Customer, with a navigation property to entity Address. Address entity contains a property "City".

I can eager load the Address entity while getting all Customers like this:

_dbSet.Customers.Include(customer => customer.Address);

This gives me all the customers, with all the Address properties eager loaded.

Of course this works fine, but the only thing i need from the Address table is the field "City", and it does not feel good to fetch all the address properties from the persistent data store (SQL Server) while not needing them.

I tried the following:

_dbSet.Customers.Include(customer => customer.Address.City);

...but this gives me a runtime exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: A specified Include path is not valid. The EntityType 'MyModel.Address'does not declare a navigation property with the name 'City'.

I understand this, since City is just a field, and not a relation to another table / entity.

But is there another way to accomplish what i want, or is it best practice to just include the whole Address entity, even if i only need the city field???

What i want is that i can use myCustomer.Address.City, without having an extra query to the database, but for examle when i use myCustomer.Address.Street, the Street property is not eager loaded, and should be additionally fetched from the database...

like image 527
Jeroen1984 Avatar asked Nov 29 '13 09:11

Jeroen1984


People also ask

Is it possible to lazy load related entities in EF6?

Even with lazy loading disabled (in EF 6), it is still possible to lazily load related entities, but it must be done with an explicit call. Use the Load () method to load related entities explicitly. Consider the following example.

What is eager loading in Entity Framework?

The techniques shown in this topic apply equally to models created with Code First and the EF Designer. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method.

How to load related entities in an entity graph explicitly?

Here you will learn how to load related entities in an entity graph explicitly. Explicit loading is valid in EF 6 and EF Core both. Even with lazy loading disabled (in EF 6), it is still possible to lazily load related entities, but it must be done with an explicit call. Use the Load () method to load related entities explicitly.

How to count related entities without loading them in Entity Framework?

Using Query to count related entities without loading them Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.


Video Answer


2 Answers

Select only the properties you want, EF will only load what's needed.

var query = _dbSet.Customers.Include(customer => customer.Address);
var data = query.Select(c => new { Customer = c, City = c.Address.City });
like image 65
Stef Heyenrath Avatar answered Oct 23 '22 18:10

Stef Heyenrath


If you are really set on using the same entity throughout your code base, then you could get around the issue using something similar to what Stef proposed:

var query = _dbSet.Customers.Include(customer => customer.Address);
var data = query
    .Select(c => new { Customer = c, City = c.Address.City })
    .ToList() //executes the IQueryable, and fetches the Customer and City (only) from the DB
    .ForEach(x => x.Customer.Address = new Address { City = x.City })
    .Select(x => x.Customer)
    .ToList();

I am very much in favour of DTOs and not using entity objects in the whole code base, but the above will give you a list of Customers which have Address objects with only the City field populated. Obviously, I make the assumption that your objects have public setters, which entity objects typically do have.

like image 32
flipchart Avatar answered Oct 23 '22 19:10

flipchart