[英]Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted
[英]the problem is delete statement affected an unexpected number of rows (0). Entities may have been deleted since entities were loaded
问题是delete语句影响了意外的行数(0)。 自加载实体以来,实体可能已被删除。
using Delete_E.DAL;
using System.Data.Entity;
namespace Delete_E
{
public partial class Form1 : Form
{
UserinfoEntities conn = new UserinfoEntities();
tableUserinfo otable = new tableUserinfo();
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//otable.id = Convert.ToInt32(dgvData.CurrentRow.Cells["id"].Value.ToString());
otable.Name = dgvData.CurrentRow.Cells[1].Value.ToString();
otable.Email = dgvData.CurrentRow.Cells[2].Value.ToString();
otable.Salary= Convert.ToDouble(dgvData.CurrentRow.Cells[3].Value.ToString());
otable.Gender = dgvData.CurrentRow.Cells[4].Value.ToString();
otable.Speciality = dgvData.CurrentRow.Cells[5].Value.ToString();
}
public void selectTable()
{
dgvData.DataSource=conn.tableUserinfoes.ToList<tableUserinfo>();
}
private void Form1_Load(object sender, EventArgs e)
{
selectTable();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult qustion = MessageBox.Show("Are you Sure to Deleted this Record ","Message Deleted",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (qustion == DialogResult.Yes)
if (conn.Entry(otable).State == EntityState.Detached)
MessageBox.Show("the sata is :"+ conn.Entry(otable).State);
conn.tableUserinfoes.Attach(otable);
conn.tableUserinfoes.Remove(otable);
conn.SaveChanges();
selectTable();
}
}
}
您的问题之一是在按钮单击事件中,
无论DialogResult
如何, selectTable
始终执行attach
, remove
和selectTable
。
我想你的意思是
private void button1_Click(object sender, EventArgs e)
{
DialogResult qustion = MessageBox.Show("Are you Sure to Deleted this Record ","Message Deleted",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (qustion == DialogResult.Yes)
{
if (conn.Entry(otable).State == EntityState.Detached)
MessageBox.Show("the sata is :"+ conn.Entry(otable).State);
conn.tableUserinfoes.Attach(otable);
conn.tableUserinfoes.Remove(otable);
conn.SaveChanges();
selectTable();
}
}
我不知道您为什么显示该消息,
以及为什么要先移除,然后再移除。
但是无论如何,如果没有{},它将永远无法按预期工作。
您正在尝试删除不是来自实体接口的对象。 您需要直接从实体(例如,使用查询)实例化此tableUserinfo
对象,或者至少使用ID(主键)创建它。
请检查以下代码:
public partial class Form1 : Form
{
UserinfoEntities conn = new UserinfoEntities();
int selectedUserInfoID; // Keep the currently selected ID of the tableUserinfo object
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Save the clicked cell's ID (assuming it comes from this expression)
selectedUserInfoID = Convert.ToInt32(dgvData.CurrentRow.Cells["id"].Value.ToString());
}
public void selectTable()
{
dgvData.DataSource=conn.tableUserinfoes.ToList<tableUserinfo>();
}
private void Form1_Load(object sender, EventArgs e)
{
selectTable();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult qustion = MessageBox.Show("Are you Sure to Deleted this Record ","Message Deleted",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (qustion == DialogResult.Yes)
{
// Ask Entity to generate the proper object with the selected ID (has to be PRIMARY KEY in that table)
tableUserinfo selectedUserInfo = conn.tableUserinfoes.Find(selectedUserInfoID);
if (selectedUserInfo != null)
conn.tableUserinfoes.Remove(selectedUserInfo); // Tell Entity to delete that object (row from database)
conn.SaveChanges(); // Commit the delete operation
selectTable();
}
}
}
创建具有正确ID的对象的另一种选择:
if (qustion == DialogResult.Yes)
{
tableUserinfo otable = new tableUserinfo{ id = selectedUserInfoID };
conn.tableUserinfoes.Attach(otable);
conn.tableUserinfoes.Remove(otable);
conn.SaveChanges();
selectTable();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.