WPF + EF。 context.SaveChanges()不起作用

时间:2014-11-18 10:41:54

标签: c# wpf datagrid frameworks entity

我是一名新的WPF开发人员,我正在为宿舍写一个WPF项目。我使用Entity Framework和DB第一种方法。在UI中,我有一个数据网格,它绑定到学生的集合。并且声明了"RowEditEnding""AddingNewItem"个事件。

所以我想要的只是能够编辑和添加新学生。 Everythings工作正常,没有错误,但SaveChanges()方法没有将实体保存到数据库。

我的MainWindow.xaml.cs代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    HostelDBEntities db = new HostelDBEntities();

    private bool isInsertMode = false;
    private bool isBeingEdited = false;

    private ObservableCollection<Student> GetStudentsList()
    {
        var list = from e in db.Students select e;
        return new ObservableCollection<Student>(list);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        studentsGrid.ItemsSource = GetStudentsList();
    }

    private void studentsGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        Student student = new Student();
        Student st = e.Row.DataContext as Student;
        if (isInsertMode)
        {
            var InsertRecord = MessageBox.Show("Do you want to add a new student?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (InsertRecord == MessageBoxResult.Yes)
            {
                student.FirstName = st.FirstName;
                student.LastName = st.LastName;
                student.Patronymic = st.Patronymic;
                db.Students.Add(student);
                db.SaveChanges();
            }
            else studentsGrid.ItemsSource = GetStudentsList();
        }

        db.SaveChanges();
    }

    private void studentsGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
    {
        isInsertMode = true;
    }
}

我的XAML代码DataGrid

<DataGrid AutoGenerateColumns="False" Name="studentsGrid" Margin="20" ItemsSource="{Binding}"
            RowEditEnding="studentsGrid_RowEditEnding"   AddingNewItem="studentsGrid_AddingNewItem" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding StudentId, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="StudentId" IsReadOnly="True" />
        <DataGridTextColumn Binding="{Binding FirstName, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="First Name" />
        <DataGridTextColumn Binding="{Binding Patronymic, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Patronymic" />
        <DataGridTextColumn Binding="{Binding LastName, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Last Name" />
    </DataGrid.Columns>
</DataGrid>

任何人都可以帮我理解如何解决它吗?创建上下文以保存对数据库的更改。

谢谢。

1 个答案:

答案 0 :(得分:0)

我会尝试更改您的XAML,以便

  

isInsertMode = true;

在您的 RowEditEnding 事件触发时调用

get。您实际上是在

上调用它的
  

AddingNewItem =“ studentsGrid_AddingNewItem”

会导致触发,即使此时您的模型不能输入任何数据,甚至也无法键入任何字符。您不能将 “无” 保存到数据库。您还可以通过在 isInsertMode = true; 之前放置一个断点来进行检查。 用

调用
  

studentsGrid_RowEditEnding()

确保当该行失去焦点时调用db.SaveChanges。