Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF&x2B;英孚。context.SaveChanges()不';行不通_C#_Wpf_Datagrid_Frameworks_Entity - Fatal编程技术网

C# WPF&x2B;英孚。context.SaveChanges()不';行不通

C# WPF&x2B;英孚。context.SaveChanges()不';行不通,c#,wpf,datagrid,frameworks,entity,C#,Wpf,Datagrid,Frameworks,Entity,我是一个新的WPF开发者,我正在为一家旅馆写一个WPF项目。我使用实体框架和DB优先的方法。在UI中,我有一个绑定到学生集合的datagrid。声明了“RowEditEnding”和“AddingNewItem”事件 所以我想要的只是能够编辑和添加新的学生。一切正常,没有错误,但SaveChanges()方法不会将实体保存到数据库中 MainWindow.xaml.cs的我的代码: public partial class MainWindow : Window { public Mai

我是一个新的WPF开发者,我正在为一家旅馆写一个WPF项目。我使用实体框架和DB优先的方法。在UI中,我有一个绑定到学生集合的datagrid。声明了
“RowEditEnding”
“AddingNewItem”
事件

所以我想要的只是能够编辑和添加新的学生。一切正常,没有错误,但
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,以便

isInsertMode=true

当您的行编辑结束事件触发时,将调用get。你真的打电话了吗

AddingNewItem=“studentsGrid\u AddingNewItem”

这会导致触发,此时您的模型甚至在您能够键入任何字符之前都没有保存任何数据。无法将“nothing”保存到数据库中。您还可以通过在isInsertMode=true之前放置断点来检查这一点。 打电话进来

学生Grid_RowEditEnding()


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

我知道你说没有错误,但是将
SaveChanges()
包装在
try..catch
块中,看看发生了什么。@Ric做的,一切都很顺利,没有错误当你添加实体但不更新时,它是否工作?@Ric否,这两种情况都不起作用
<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>