实体框架 - 负载仅选定的属性 [英] Entity Framework - Load only Selected properties
问题描述
我有经验没有手它EF,因此不知道问题的relevence。
假设我有一个名为学生表(StudentId,名称,用户名,地址,出生日期,DEPTID,NavigationProp1Id ....)和Depatement表(Deptd,DEPTNAME,NavigationProPid)。因此,如果一个表结构如下这样的,当我使用'contex.Studnets'我可以得到所有prpoerties它包括导航性能,如果表2有其他的导航性能,还可以加载一起。我对么 ?
I have no hands on experience it EF and hence dont know the relevence of the question. Suppose i have tables named Student (StudentId, Name, Username, Address, DOB, DeptId, NavigationProp1Id.... ) and in Depatement table (Deptd, DeptName., NavigationProPid). So if a table structure follows like this, when I use 'contex.Studnets ' i can get all prpoerties along with it including navigation properties and if table 2 has other navigation properties it can also be loaded. Am I correct ?
如果这样是否造成任何性能问题?我可以从实体只加载所选属性只喜欢用户名,从Studnet enity?
If so whether it cause any performance issue? Can I load only selected properties from Entity like only UserName,Addres from Studnet enity?
任何帮助将大大AP preciated
Any help will be greatly appreciated
日Thnx和问候
骨多
推荐答案
没有导航属性,不立即装载。它们加载任何明示如果你使用包含
方法,或懒洋洋地在您访问的第一次(这也是为什么你看到他们在调试器=访问通过调试器造成的懒惰加载)。
No navigation properties are not loaded immediately. They are loaded either explicitly if you use Include
method or lazily when you access them for the first time (that is also reason why you see them in debugger = access through debugger caused lazy loading).
您可以加载唯一入选的特性 - 它被称为投影。该限制是不能用的属性集加载学生的实体。你需要无论是新类或匿名类型:
You can load only selected properties - it is called projection. The limitation is that you cannot load Student entity with subset of properties. You need either new class or anonymous type:
var query = context.Students.Select(x => new
{
x.UserName,
x.Address
});
这篇关于实体框架 - 负载仅选定的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!