1

我是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



            }
        }
    }
4

1 回答 1

0

你有大约三个选择

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