EF 5:处理上下文时丢失了OriginalValues

时间:2013-02-14 09:13:58

标签: wpf frameworks entity dbcontext

我是EF的新手。因此,为了测试乐观并发性,我使用了EF 5,WPF和已经创建的数据库.WPF项目包含MainWindow TextBox1用于通过主键搜索客户,TextBox2用于客户名称编辑,textblock (txtLog)记录字段值和保存按钮。要模拟并发访问,请在SQL Server 2008 Management Studio中执行查询 “update client set r01_nomcli = 'GGGGG' where r01_codcli = '4112010002';” 在点击保存按钮之前。它工作正常,DbUpdateConcurrencyException被触发,但实体的原始值丢失并采用相同的值 作为当前价值的那些。这是因为“使用”块导致实体的分离。要解决这个问题,我必须在窗口的构造函数之前声明上下文并在“Window_Loaded”事件中实例化它 是否有另一个使这??? 帮助了解使用EF的最佳实践以及如何使用EF 5构建可重复使用的DAL。 谢谢。 代码下方:

 public partial class OptimisticConcurrency : Window
{

//client is an entity

    client _currentClient = null;

    public OptimisticConcurrency()
    {
        InitializeComponent();
    }


    //**************************TextBox1 PreviewKeyDown Event Handler
    private void TextBox_PreviewKeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {

            using (dbleaseEntities context = new dbleaseEntities())
            {
                _currentClient = context.client.Find(txtCode.Text);
                if (_currentClient == null)
                    MessageBox.Show("Customer " + txtCode.Text + " not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                else
                    this.DataContext = _currentClient;
            }
        }
    }


//*****************Save Button Click Event Handler

private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        using (dbleaseEntities context = new dbleaseEntities())
        {
            context.Configuration.ValidateOnSaveEnabled = false;
            try
            {
                context.Entry(_currentClient).State = System.Data.EntityState.Modified;
                context.SaveChanges();
                MessageBox.Show("Success.", "Save", MessageBoxButton.OK,    MessageBoxImage.Information);
            }
            catch (DbEntityValidationException ex)
            {
                string ss = "";
                foreach (var error in ex.EntityValidationErrors)
                    ss = string.Join(Environment.NewLine, error.ValidationErrors.Select(v => v.ErrorMessage));
                MessageBox.Show(ss, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }

            catch (DbUpdateConcurrencyException ex)
            {
                string StrValues= "";
                DbPropertyValues ov = ex.Entries.Single().OriginalValues;
                DbPropertyValues cv = ex.Entries.Single().CurrentValues;
                DbPropertyValues dv = ex.Entries.Single().GetDatabaseValues();

                StrValues=  "Original value  : " + ov["r01_nomcli"] + Environment.NewLine +
                            "Current value   : " + cv["r01_nomcli"] + Environment.NewLine +
                            "Database value  : " + dv["r01_nomcli"];

               txtLog.Text = StrValues



            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您有大约三个选项

  1. 做你现在正在做的事情 - 即保持对象连接到上下文
  2. 使用自我跟踪实体:http://blogs.msdn.com/b/efdesign/archive/2009/03/24/self-tracking-entities-in-the-entity-framework.aspx
  3. 使用GetDatabaseValues