的EntityFramework - 实体代理错误 [英] EntityFramework - Entity proxy error

查看:310
本文介绍了的EntityFramework - 实体代理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的EntityFramework的系统上,并已超过12monts目前,该项目已顺利,直到昨天,在那里我现在有一个奇怪的错误,我不知道为什么它发生。
我什么都不做什么,我以前做过不同的,但一旦我加载实体问题,并尝试访问任何子实体我收到以下错误:

 存储在代理不引用相同的代理



中的实体包装

任何人都可以摆脱任何光线什么这实际上意味着,什么会导致此?



显示我的代码,并没有真正的帮助。



下面是代码的简化版本:

  VAR报价=新QuoteHelper()GetById(订单ID ); 
变种updatedQuotes =新的提供商()GetExportQuotes(quote.DeparturePoint.Id,quote.DestinationPoint)。



访问DeparturePoint和DestinationPoint但报价负载时正确的,而且所有属性都加载时,会出现错误。



实体引用看起来有点像这样:

 公共类引用: BaseQuote,ICloneable 
{
公众的Guid DeparturePointId {搞定;组; }
公共虚拟的LocationPoint DeparturePoint {搞定;组; }

公众的Guid DestinationPointId {搞定;组; }
公共虚拟的LocationPoint DestinationPoint {搞定;组; }
}


解决方案

这对我来说太当我试图实现我的实体ICloneable使用MemberwiseClone克隆它。我是用我自己的实例化时的实体伟大的工作。然而,当我用这个克隆已被使用EF加载的实体,我每当我试着将它添加到DbSet(或其他各个部分)得到这个错误。



一些挖后,我发现,当您克隆EF加载实体,你克隆代理类为好。其中一个代理类随身携带的一件事就是到包装FO给定的实体的引用。由于浅拷贝只复制到包装的引用,你突然有一个具有相同的包装实例两个实体。



在这一点上,EF认为你已经创建或借用你的实体不同的代理类,它假定是恶作剧,并阻止你的目的。



修改



下面是我创建的解决此问题的一个片段。请注意,这会做复制只是EF物业公平的工作,但它并不完美。请注意,您需要,如果你有必须同时复制私有字段来修改它,但你的想法。



<预类=郎-CS prettyprint,覆盖> ///<总结>
///使一个实体对象的浅表副本。这很像一个MemberwiseClone
///而是直接实例化一个新的对象,只复制与
工作/// EF和不具备NotMappedAttribute属性。
///< /总结>
///< typeparam NAME =TEntity方式>的实体类型< / typeparam>
///< PARAM NAME =源>在源实体LT; /参数>
公共静态TEntity ShallowCopyEntity< TEntity>(TEntity源)其中TEntity:类,新的()
{

//从EF获取属性是读/写,并没有标明witht他NotMappedAttribute
VAR sourceProperties = typeof运算(TEntity)已
.GetProperties()
。凡(p => p.CanRead和放大器;&安培; p.CanWrite和放大器;&安培;
p.GetCustomAttributes(typeof运算(System.ComponentModel.DataAnnotations.NotMappedAttribute),TRUE)。长度== 0);
变种newObj =新TEntity();

的foreach(在sourceProperties VAR属性)
{

//复制价值
property.SetValue(newObj,property.GetValue(源,NULL) , 空值);

}

返回newObj;

}


I am working on a system using Entityframework and have been for over 12monts now, and the project has been going well, up until yesterday, where I have now got a strange error which I have no idea why it occurs. I am doing nothing different to what I have done before, but once I load the entity in question and try to access any child entities I get the following error:

The entity wrapper stored in the proxy does not reference the same proxy

Can anyone shed any light on what this actually means and what would cause this?

Showing my code doesnt really help.

Here is a simplified version of the code:

var quote = new QuoteHelper().GetById(orderId);
var updatedQuotes = new Provider().GetExportQuotes(quote.DeparturePoint.Id,quote.DestinationPoint);

The error occurs when accessing DeparturePoint and DestinationPoint but Quote loads correctly, and all properties are loaded.

The entity Quote looks a little like this:

public class Quote : BaseQuote, ICloneable
{
     public Guid DeparturePointId { get; set; }
     public virtual LocationPoint DeparturePoint{ get; set; }

     public Guid DestinationPointId { get; set; }
     public virtual LocationPoint DestinationPoint{ get; set; }
}

解决方案

This happened to me too when I tried to implement ICloneable on my entity and cloned it using MemberwiseClone. Worked great when I was using entities that I instantiated myself. However, when I used this to clone an entity that had been loaded using EF, I got this error whenever I tried to add it to a DbSet (or in various other parts).

After some digging, I found that when you clone an EF-loaded entity, you're cloning the proxy class as well. One of the things a proxy class carries around is a reference to the wrapper fo the given entity. Because a shallow copy only copies a reference to the wrapper, you suddenly have two entities that have the same wrapper instance.

At this point, EF thinks you've created or borrowed a different proxy class for your entity which it assumes is for purposes of mischief and blocks you.

Edit

Here's a snippet that I created to work around this problem. Note that this will do a fair job of copying just the EF properties, but it's not perfect. Note that you'll need to modify it if you have private fields that must be copied as well, but you get the idea.

    /// <summary>
    /// Makes a shallow copy of an entity object. This works much like a MemberwiseClone
    /// but directly instantiates a new object and copies only properties that work with
    /// EF and don't have the NotMappedAttribute.
    /// </summary>
    /// <typeparam name="TEntity">The entity type.</typeparam>
    /// <param name="source">The source entity.</param>
    public static TEntity ShallowCopyEntity<TEntity>(TEntity source) where TEntity : class, new()
    {

        // Get properties from EF that are read/write and not marked witht he NotMappedAttribute
        var sourceProperties = typeof(TEntity)
                                .GetProperties()
                                .Where(p => p.CanRead && p.CanWrite &&
                                            p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.NotMappedAttribute), true).Length == 0);
        var newObj = new TEntity();

        foreach (var property in sourceProperties)
        {

            // Copy value
            property.SetValue(newObj, property.GetValue(source, null), null);

        }

        return newObj;

    }

这篇关于的EntityFramework - 实体代理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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