Jah*_*Can 4 c# entity-framework lazy-loading navigation-properties
场景:我想在数据库中添加一个具有导航属性的实体,该实体具有导航属性......等等.基本上,数据库中的表是相互连接的 - 所有这些表.
我使用EF4.3和上下文/请求模式,所以我不想启用Lazy加载; 它只需要花费太多时间来加载我需要的实体.到目前为止,我已经知道除了使用这样的include方法之外别无他法.
context.Set<TEntity>().include("navproperty1").include("navproperty1.navproperty1.1")... and so on.
Run Code Online (Sandbox Code Playgroud)
这样可维护性会很糟糕,而且它是很多代码,但是如果我不想手动为每个实体类型编写所有包含,还有其他方法吗?
这里有很多问题.我会尝试解决每一点.
首先,延迟加载并不总是更快.特别是如果你正在加载所有的关系.
第二,始终避免"魔术弦".我不知道Include接收lambda表达式的方法(它是一个IQueryable扩展)是否可用于EF 4.3.如果不是,您应该自己实现,如此处所示并使用:
context.Set<TEntity>().include(t => t.NavProp)
Run Code Online (Sandbox Code Playgroud)
"A" entity" has 1 : n relation to "B" entity but "B" entity has n : m relation to "C" entity. And if I wouldnt inlcude "C" to "A" and then try to call context.SaveChanges() then all the data would lost between "B" and "C"
I don't really know what you meant. But, if you want to select a sub-navigation property which belongs to an item in a list you should use this in EF 5: (not sure if it works in 4.3)
context.Set<TEntity>().Include(t => t.Collection.Select(c => c.SubProp))
Run Code Online (Sandbox Code Playgroud)
Other expressions can be found here
If you clarify on that quote maybe I can help more.
| 归档时间: |
|
| 查看次数: |
12127 次 |
| 最近记录: |