C#实体框架 - 数据绑定组合框中不能反映.SaveChanges后所做的更改() [英] C# Entity Framework - Data bound combobox does not reflect changes made after .SaveChanges()

查看:101
本文介绍了C#实体框架 - 数据绑定组合框中不能反映.SaveChanges后所做的更改()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这已被我发疯了最后两天!

我是新的C#a和已经通过编写一个简单的应用程序教自己。

我有一个简单的表格,包括组合框和两个文本框。该组合框包含了数据库表中的实体列表。文本框允许用户添加新的条目。这只不过是名称(第一和姓)的列表。

在形式有三个按钮添加,修改和删除。

我使用数据绑定和WPF幕后。

好吧我的问题是这样的..

有关的都删除和修改操作一切正常,我期望的那样。该数据库相应的改变和(重要的)组合框即刻反映到数据绑定实体所做的更改。

但是当我创建并添加一个新的实体数据库与新产品的更新correclty但组合框不显示在列表中的新实体(名称)。你要退出的形式和才能看到的组合框正确反映数据库中的表与新添加的项目在回来的。

谁能告诉我正确的做法是什么让数据绑定控件,以反映其绑定到表的INSERT变化?

下面...

相关code片段

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
    {
        this.myContext =新myEntities();
        //表的内容绑定到ComboBox
        myComboBox.DataContext = myContext.myPeople;
    }     私人无效myComboBox_SelectionChanged(对象发件人,SelectionChangedEventArgs E)
    {
        //更新文本框以反映当前选定的名字
        this.person = myComboBox.SelectedItem为myPerson;        如果(this.person!= NULL)
        {
            tbFirstName.Text = this.person.Firstname;
            tbSurname.Text = this.person.Surname;
        }
    }    //用户动作...    如果(userAction == crudAction.Modify)
    {
         //通过实体框架更新
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         味精=人的细节修饰;
    }    如果(userAction == crudAction.Add)
    {
         人=新myPerson();
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         person.idPeople = 0; //自动增量分贝关键
         myContext.myPeople.AddObject(人);         味精=新增的人物;
    }    如果(userAction == crudAction.Delete)
    {
         myContext.myPeople.DeleteObject(人);
         味精=人已删除;
    }
    myContext.SaveChanges();


解决方案

是否得到,如果你在运行后重新查询数据库更新的SaveChanges()

  VAR测试= myContext.myPeople.ToList();

编辑:

的ItemsSource 组合框再次来代替:

  myComboBox.ItemsSource = context.myPeople.ToList();

Ok, this has been driving me nuts for the last two days!

I'm new to C# aand have been teaching myself via writing a simple app.

I have a simple form that comprises of a combobox and two text boxes. The combobox contains a list of the entities in the database table. The text boxes allow the user to add new entries. It is simply a list of names (first and surname).

On the form are three buttons to Add, Modify and Delete.

Behind the scenes I'm using databinding and WPF.

Ok my problem is this..

For both the delete and the modify operation everything works as I would expect. The database is changed accordingly and (importantly) the combobox INSTANTLY reflects the changes made to the databound entity.

BUT when I create and add a new entity the database is updated correclty with the new item BUT the combobox does NOT show the new entity (name) in its list. You have to exit the form and come back in in order to see the combobox correctly reflect the database table with the newly added item.

Can someone tell me what the correct practise is for getting a databound control to reflect an INSERT change on the table it is bound to?

relevant code snippets below...

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.myContext = new myEntities();
        // bind the contents of the table to the combobox
        myComboBox.DataContext = myContext.myPeople;
    }

     private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Update the text boxes to reflect the currently selected name
        this.person = myComboBox.SelectedItem as myPerson;

        if (this.person != null)
        {
            tbFirstName.Text = this.person.Firstname;
            tbSurname.Text = this.person.Surname;
        }
    }

    //User actions...

    if (userAction == crudAction.Modify)
    {
         // Update via the Entity Framework
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         msg = "Person details modified";
    }

    if (userAction == crudAction.Add)
    {
         person = new myPerson();
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         person.idPeople = 0; //Autoincremented db key
         myContext.myPeople.AddObject(person);

         msg = "New person added";
    }

    if (userAction == crudAction.Delete)
    {
         myContext.myPeople.DeleteObject(person);
         msg = "Person deleted";
    }
    myContext.SaveChanges();

解决方案

Does it get updated if you requery the DB after running SaveChanges()?

var test = myContext.myPeople.ToList();

Edit:

Set the ItemsSource for the ComboBox again instead:

myComboBox.ItemsSource = context.myPeople.ToList();

这篇关于C#实体框架 - 数据绑定组合框中不能反映.SaveChanges后所做的更改()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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