实体框架POCO - 如何防止一个错误,如果一个字段更新两次? [英] Entity Framework POCO - how to prevent an error if a field is updated twice?

查看:522
本文介绍了实体框架POCO - 如何防止一个错误,如果一个字段更新两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用模型中的一配置中使用POCO实体框架。我有一个稍微非标准模型 - 通过定制.TT文件生成自定义属性响应的的.edmx使我能够触发记录更新某些属性的NotifyPropertyChanged事件 - 就像这导致的一类有效看起来有点这样的:

I am getting started with Entity Framework using POCO in a model-first configuration. I have a slightly non-standard model - generated by customising the .tt file to respond to custom properties in the .edmx enabling me to trigger a NotifyPropertyChanged event for logging updates to certain properties - which results in a class that effectively looks a little like this:

public partial class MyClass: INotifyPropertyChanged
{
    /// <summary>
    /// Catches events to be added to the UserLog
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notifies any event listeners attached to the PropertyChanged event that a loggable field
    /// update has occurred.
    /// </summary>
    /// <param name="eventType">The type of the field.</param>
    /// <param name="message">The message to record in the logs</param>
    private void NotifyFieldUpdate(string eventType, string message)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new LogEventArgs(eventType, message));
        }
    }


  private  string _myField;
  public virtual string MyField
    {
      get
      {
        return _myField;
      }
      set 
      {
        if ( _myField != value )
          {
        _myField = value;
        NotifyFieldUpdate( "FIELDCHANGE", String.Format("MyField changed to {0}", value) );
          }
        }
      }
  }



。事件处理程序,然后在部分类,以避免不必要的重写的另一部分构造

The event handler is then configured in another part of the partial class, to avoid unnecessary overwrites.

我的问题是,如果该被更新两次,它翻倒:

The problem I have is that if that gets updated twice, it falls over:

  public void TestBehaviour(ObjectContext currentContext)
  {
    MyClass testMe = FetchFromObjectContext(currentContext);
    testMe.MyField = "Hello";
    currentContext.SaveChanges();
    testMe.MyField = "Goodbye";
  }



我调用这些方法的第二个的那一刻,我打了以下错误:

The moment I call the second of those methods, I hit the following error:

EntityMemberChanged或EntityComplexMemberChanged被称为无需先调用EntityMemberChanging或EntityComplexMemberChanging在同一变更跟踪器具有相同的属性名称。有关报告的适当变化,请参阅实体框架文档。

EntityMemberChanged or EntityComplexMemberChanged was called without first calling EntityMemberChanging or EntityComplexMemberChanging on the same change tracker with the same property name. For information about properly reporting changes, see the Entity Framework documentation.

我已经尝试使用 currentContext.DetectChanges( ) currentContext.Refresh(...)但是这真的是猴子的编码,因为我不知道究竟是怎么回事。

I have tried using currentContext.DetectChanges() and currentContext.Refresh( ... ) but that is really monkey coding as I don't know what exactly is going on.

我的第一个问题是:是什么原因造成的问题,我有什么,以避免这种类型的错误与ObjectContext的呢?这似乎很合理,我认为领域可以随时更新时间,我会恨我的系统翻倒,如果它发生了两次。

My first question is: What is causing the problem and what do I have to do with the ObjectContext in order to avoid this type of error? It seems quite plausible to me that fields could be updated from time to time and I would hate for my system to fall over if it happened twice.

我的第二个,或许更深入的问题是:我是否可以使用 INotifyPropertyChanged的接口,当它出现的类射击这些接近这一切都错了 EntityMemberChanged 事件了吗?我想这是因为ObjectContext的是创建的Proxy-确实,在某些情况下,这些方法可能不可用,如果我没有我自己的通知是什么意思?

My second, perhaps more in-depth question is: Am I approaching this all wrong by using the INotifyPropertyChanged interface when it appears that the class is firing these EntityMemberChanged events already? I assume this is because the ObjectContext is creating a proxy- does that mean in some cases those methods are likely to be unavailable if I don't have my own notifications?

推荐答案

原来,问题的根本原因是,因为我怀疑,由创建代理做我的的ObjectContext

It turns out that the root cause of the problem was, as I suspected, to do with the creation of proxies by my ObjectContext.

要解决该错误, - 在我的情况 - 因此解决这个问题,我只需要添加

To resolve the error and - in my case - consequently solve the problem, I just needed to add

currentContext.ContextOptions.ProxyCreationEnabled = false;



我ObjectContext的创建,并防止它创建代理。这可能花了我一点点的方便,但考虑到他们似乎有些脆弱和不透明的,我认为它为我节省了很多的麻烦暂时。

to my ObjectContext creation and it prevents it creating proxies. This may cost me a little convenience, but given that they seem somewhat fragile and opaque I think it saves me a lot of hassle for the time being.

有用的资源: MSDN与POCO实体

这篇关于实体框架POCO - 如何防止一个错误,如果一个字段更新两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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