繁体   English   中英

EntityFramework - 实体代理错误

[英]EntityFramework - Entity proxy error

我正在使用 Entityframework 开发一个系统,现在已经超过 12 个月了,项目进展顺利,直到昨天,我现在遇到了一个奇怪的错误,我不知道它为什么会发生。 我所做的与之前所做的没有什么不同,但是一旦我加载了有问题的实体并尝试访问任何子实体,我就会收到以下错误:

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

任何人都可以阐明这实际上意味着什么以及会导致这种情况的原因吗?

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

这是代码的简化版本:

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

访问 DeparturePoint 和 DestinationPoint 时发生错误,但 Quote 加载正确,并且加载了所有属性。

实体 Quote 看起来有点像这样:

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; }
}

当我尝试在我的实体上实现 ICloneable 并使用 MemberwiseClone 克隆它时,这也发生在我身上。 当我使用我自己实例化的实体时效果很好。 但是,当我使用它来克隆使用 EF 加载的实体时,每当我尝试将它添加到 DbSet(或其他各种部分)时,我都会收到此错误。

经过一番挖掘,我发现当您克隆一个 EF 加载的实体时,您也在克隆代理类。 代理类携带的一件事是对给定实体的包装器的引用。 由于浅拷贝仅复制对包装器的引用,因此您会突然拥有两个具有相同包装器实例的实体。

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

编辑

这是我为解决此问题而创建的一个片段。 请注意,这可以很好地复制 EF 属性,但它并不完美。 请注意,如果您也有必须复制的私有字段,则需要修改它,但您明白了。

    /// <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;

    }

上述解决方案可能会出现错误“已检测到关系 y 的角色 x 的冲突更改”。 我使用此方法实现了该错误;

 public virtual TEntity DetachEntity(TEntity entityToDetach)
    {
        if (entityToDetach != null)
            context.Entry(entityToDetach).State = EntityState.Detached;
        context.SaveChanges();
        return entityToDetach;
    }

我希望它也适合你。

我是这样解决的。

using (var ctx = new MyContext())
      {
         ctx.Configuration.ProxyCreationEnabled = false;

         return ctx.Deferrals.AsNoTracking().Where(r => 
         r.DeferralID.Equals(deferralID)).FirstOrDefault();
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM