【发布时间】:2019-02-15 00:32:50
【问题描述】:
问题是删除语句影响了意外的行数 (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();
}
}
}
【问题讨论】:
-
你知道
conn.tableUserinfoes.Attach(otable);和它下面的其他statents,总是被执行,不管DialogResult? -
DataRow 删除方法根据文档仅删除一行:“从 System.Collections.Generic.List
中删除特定对象的第一个匹配项。”你有共享服务器吗?请参阅:docs.oracle.com/cd/B19306_01/server.102/b14200/…
标签: c# sql-server entity-framework