C# 为什么 EF 返回代理类而不是实际实体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9501471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is EF returning a proxy class instead of the actual entity?
提问by Malcolm O'Hare
I'm having trouble with entity framework returning Proxies when I want the actual entity class. The first time I run my code everything runs properly (no proxies), but every iteration afterwards one of my DbSets always returns proxies instead of the actual type.
当我想要实际的实体类时,实体框架返回代理时遇到问题。我第一次运行我的代码时,一切都正常运行(没有代理),但是之后的每次迭代我的一个 DbSet 总是返回代理而不是实际类型。
I dispose of the context after every iteration, so I don't understand why the first time through it works, and every time after doesn't.
我在每次迭代后处理上下文,所以我不明白为什么第一次通过它有效,而每次之后都没有。
My code fails on this line. All my POCOs have the Table attribute set, but because it is returning a proxy class there is no table attribute.
我的代码在这一行失败。我所有的 POCO 都设置了 Table 属性,但是因为它返回的是代理类,所以没有 table 属性。
TableAttribute attrib = (TableAttribute)attributes.Single();
Is there some behind the scenes static magic in the DbContext that lives after I destroy the object?
在我销毁对象后,DbContext 中是否有一些幕后静态魔法?
I move my objects into memory using the following
我使用以下方法将对象移动到内存中
MajorClasses = ctx.MajorClasses.ToArray();
I also tried
我也试过
MajorClasses = ctx.MajorClasses.AsNoTracking().ToArray();
In my OnModelCreating I have the following set
在我的 OnModelCreating 我有以下设置
base.Configuration.ProxyCreationEnabled = false;
base.Configuration.LazyLoadingEnabled = false;
采纳答案by cadrell0
You can set ObjectContext.ContextOptions.ProxyCreationEnabledto false. This will prevent you from using some of EFs fancy features like lazy loading and I believe change tracking.
您可以设置ObjectContext.ContextOptions.ProxyCreationEnabled为false。这将阻止您使用一些 EF 奇特的功能,例如延迟加载,我相信更改跟踪。
As far as your app cares, it should be able to treat the proxies just like the types they represent. Is there a specific issue you are having?
就您的应用而言,它应该能够像对待代理所代表的类型一样对待代理。您有什么具体问题吗?
Edit
编辑
We have some code that requires the POCO type instead of the proxy type and we do the following to detect if the current type is a proxy.
我们有一些代码需要 POCO 类型而不是代理类型,我们执行以下操作来检测当前类型是否为代理。
if (entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies")
{
entityType = entityType.BaseType;
}
回答by Davin Tryon
By default, EF uses Change Trackingand uses an in-memory cache of all entities. You can use different Merge Options when working with EF. By default, EF 4.1 is set to AppendOnly Merge Option. As I understand, this means that if you have already queried an entity, subsequent queries will get the entity from the cache (if there are no detected changes in the database). So you might be seeing the cached entity coming back.
默认情况下,EF 使用更改跟踪并使用所有实体的内存缓存。使用 EF 时,您可以使用不同的合并选项。默认情况下,EF 4.1 设置为 AppendOnly 合并选项。据我了解,这意味着如果您已经查询了一个实体,后续查询将从缓存中获取该实体(如果在数据库中没有检测到更改)。所以你可能会看到缓存的实体回来了。
In EF 4.1, you can use NoTracking Merge Option. This will go to the database for every call.
在 EF 4.1 中,您可以使用 NoTracking Merge Option。这将在每次调用时转到数据库。
回答by BizarroDavid
To turn off proxy creation in Entity Framework 5 you can use the following,
要在 Entity Framework 5 中关闭代理创建,您可以使用以下命令,
_dbContext.Configuration.ProxyCreationEnabled = false;
Simply set this property once before using the context to pull data.
在使用上下文提取数据之前,只需设置一次此属性。
回答by Kautsky Lozano
In EF 6.1.3 you can get the right type using
在 EF 6.1.3 中,您可以使用
using (var context = new BloggingContext()) {
var blog = context.Blogs.Find(1);
var entityType = ObjectContext.GetObjectType(blog.GetType());
}
Note that if the type passed to GetObjectType is an instance of an entity type that is not a proxy type then the type of entity is still returned. This means you can always use this method to get the actual entity type without any other checking to see if the type is a proxy type or not.
请注意,如果传递给 GetObjectType 的类型是不是代理类型的实体类型的实例,则仍会返回实体类型。这意味着您始终可以使用此方法获取实际实体类型,而无需进行任何其他检查以查看该类型是否为代理类型。
From MSDN
来自MSDN
回答by Hassan Raza
simple solution is you are missing some object that must be included and also do this one before getting values
简单的解决方案是您缺少一些必须包含的对象,并且在获取值之前也执行此操作
_dbContext.Configuration.ProxyCreationEnabled = false;
回答by cdsln
In my case this issue was fixed by setting Lazy Loading Enabledto false.
在我的情况下,此问题已通过设置Lazy Loading Enabled为false.
- Open the .edmx (diagram)
- Hit F4 to bring up the properties
- Set
Lazy Loading Enabledtofalse - Save and rebuild
- 打开 .edmx(图表)
- 按F4调出属性
- 设置
Lazy Loading Enabled为false - 保存并重建

