In EF6, I have an object client with a navigation property for an organization address. The address object contains the City property.
I can load the Address object, getting all clients as follows:
_dbSet.Customers.Include(customer => customer.Address);
This gives me all the clients, with all the address properties being loaded.
Of course, this works fine, but the only thing I need in the Address table is the City field, and itβs not very good to get all the address properties from the persistent data store (SQL Server), while they are not required.
I tried the following:
_dbSet.Customers.Include(customer => customer.Address.City);
... but this gives me an exception at runtime:
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 because City is just a field, not a relation to another table / entity.
But is there any other way to accomplish what I want, or is it best to just include the entire Address object, even if I only need the city field ???
I want me to be able to use myCustomer.Address.City without having an extra query for the database, but for example, when I use myCustomer.Address.Street, the Street property is not loading and should be optional from the database ...