JK.*_*JK. 7 c# entity-framework proxy-classes poco
在实体框架4中是否可以选择在没有使用代理类的情况下将某些查询加载到POCO中?(出于缓存该对象的目的,以备将来只读使用).我正在使用存储库 - 服务模式.
我的意思是:
var order = _orderService.GetById(1);
// after order is loaded then we can see in the debugger that:
// order.Customer is of type System.Data.Entity.DynamicProxy.Customer_17631AJG_etc
Run Code Online (Sandbox Code Playgroud)
我想要的是order.Customer实际使用POCO类型MyApp.Models.Entities.Customer而不是该类型的代理.
编辑:根据Ladislav建议在存储库中添加"GetUnproxied"方法,我做了这个改动:
// this is the current method that must return a DynamicProxy
public IQueryable<T> GetQuery()
{
return ObjectSet.AsQueryable();
}
// this is the new additional method that must return the plain POCO
public IQueryable<T> GetReadOnly()
{
ObjectContext.ContextOptions.ProxyCreationEnabled = false;
var readOnly = ObjectSet.AsQueryable();
ObjectContext.ContextOptions.ProxyCreationEnabled = true;
return readOnly;
}
Run Code Online (Sandbox Code Playgroud)
它是否正确?
它对我来说看起来不安全.两种方法都使用相同的ObjectContext实例,因此有可能ProxyCreationEnabled == false在一个线程上发生然后public IQueryable<T> GetQuery()在另一个线程上调用 - 这突然意味着代理方法可以返回非代理对象.
Lad*_*nka 16
在查询数据以关闭代理创建之前使用此选项
context.ContextOptions.ProxyCreationEnabled = false;
Run Code Online (Sandbox Code Playgroud)
我认为它也可以在EDMX设计师的全球范围内关闭.
这适用于ObjectContext.随着DbContext代码:
context.Configuration.ProxyCreationEnabled = false;
Run Code Online (Sandbox Code Playgroud)
加上我在edmx设计师中看不到任何选项
| 归档时间: |
|
| 查看次数: |
9216 次 |
| 最近记录: |