EF6渴望加载相关实体的单个属性 [英] EF6 eager load single property of related entity

查看:73
本文介绍了EF6渴望加载相关实体的单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF6中,我有一个实体Customer,其导航属性为实体Address。地址实体包含属性城市。

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

我可以急于加载Address实体,同时让所有客户都这样:

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

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

这给了我所有的客户,并渴望加载所有的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.

我尝试了以下操作:

_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'.

我理解这一点,因为City只是一个字段,而不是与另一个表/实体的关系。

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

但是还有另一种方法可以实现我想要的功能吗,或者即使我只需要city字段,最好也只包括整个Address实体?

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???

我想要的是我可以使用myCustomer.Address.City,而无需对数据库进行额外查询,但是例如,当我使用myCustomer.Address.Street时, Street属性不是急切加载的,应该另外从数据库中获取...

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...

推荐答案

如果您是 确实在整个代码库中使用相同的实体,那么您可以使用类似于Stef建议的方法来解决该问题:

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();

我非常支持DTO,而在整个代码库,但是上面的代码将为您提供 Customer 的列表,这些列表具有 Address 个对象,其中只有城市字段。显然,我假设您的对象具有公共设置器,而实体对象通常具有公共设置器。

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.

这篇关于EF6渴望加载相关实体的单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆