C# SaveChanges适用于单个CRUD操作,但不适用于混合操作

C# SaveChanges适用于单个CRUD操作,但不适用于混合操作,c#,entity-framework,savechanges,C#,Entity Framework,Savechanges,我是实体框架的新手,我有一个愚蠢的问题 我用数据填充datagrid视图,然后进行更改,最后按下按钮保存更改。 只要我只应用相同的操作,例如只要我添加新记录,将数据保存到数据库就没有问题。但是,当我混合操作时,例如,当我添加一条新记录,并连续删除另一条记录,然后按下保存按钮时,更改将丢失,并且两个操作都不会对数据库执行 在按钮单击事件上,我只有ctx.SaveChanges();在试抓块上! 发生了什么? 实体框架是否有会话之类的东西,或者在每次操作中都会发生变化的东西 代码如下: 使用系统;

我是实体框架的新手,我有一个愚蠢的问题

我用数据填充datagrid视图,然后进行更改,最后按下按钮保存更改。 只要我只应用相同的操作,例如只要我添加新记录,将数据保存到数据库就没有问题。但是,当我混合操作时,例如,当我添加一条新记录,并连续删除另一条记录,然后按下保存按钮时,更改将丢失,并且两个操作都不会对数据库执行

在按钮单击事件上,我只有
ctx.SaveChanges()
;在试抓块上! 发生了什么? 实体框架是否有会话之类的东西,或者在每次操作中都会发生变化的东西

代码如下:
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间HiClinic
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
Hiclinicenties ctx=新Hiclinicenties();
私有void Form1\u加载(对象发送方、事件参数e)
{
BindingSource bs=新的BindingSource();
bs.DataSource=(从ctx.People中的r选择r.ToList();
bs.AddingNew+=新的AddingNewEventHandler(personBindingSource\u AddingNew);
bs.AllowNew=真;
dataGridView1.DataSource=bs;
}
void personBindingSource\u AddingNew(对象发送方,AddingNewEventArgs e)
{
var p=新人();
ctx.People.Add(p);
e、 NewObject=p;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
尝试
{
ctx.SaveChanges();
}
捕获(例外情况除外)
{
WriteLine(“{0}捕获异常。”,ex);
}
}
私有void dataGridView1_UserDeletingRow(对象发送者,DataGridViewRowCancelEventArgs e)
{
var p=long.Parse(例如Row.Cells[“PersonID”].FormattedValue.ToString());
ctx.People.Remove(ctx.People.First(c=>c.PersonID==p));
}
}
}

您可以将代码发布到问题中吗?要删除,您需要先调用DeleteObject()。如果您使用的是没有实体状态信息的POCO对象,您需要将正确状态的实体添加到上下文中,因此如果您只将实体添加到上下文中并保存更改,实体框架将不知道如何处理每个实体。是否有任何错误?有例外吗?没有错误没有例外。如果我只是添加行并保存,或者删除行并保存,那么效果会非常好,但当我混合添加和删除时,DB上不会发生任何事情
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HiClinic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        HiClinicEntities ctx = new HiClinicEntities();
        private void Form1_Load(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = (from r in ctx.People select r).ToList();
            bs.AddingNew += new AddingNewEventHandler(personBindingSource_AddingNew);
            bs.AllowNew = true;
            dataGridView1.DataSource = bs;
        }
        void personBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            var p = new Person();
            ctx.People.Add(p);
            e.NewObject = p;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }
        }

        private void dataGridView1_UserDeletingRow(object sender,DataGridViewRowCancelEventArgs e)
        {
            var p = long.Parse(e.Row.Cells["PersonID"].FormattedValue.ToString());
            ctx.People.Remove(ctx.People.First<Person>(c=>c.PersonID == p));
        }
    }
}