【问题标题】:the problem is delete statement affected an unexpected number of rows (0). Entities may have been deleted since entities were loaded问题是删除语句影响了意外的行数(0)。自加载实体后,实体可能已被删除
【发布时间】: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


【解决方案1】:

您的问题之一在于按钮单击事件,
它将始终执行attachremoveselectTable,而不管DialogResult

我想你的意思是这样的

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();
    }
}

我不知道您为什么要显示该消息,
以及为什么要在移除之前先附加。
但无论如何,如果没有 {},它永远不会按预期工作。

【讨论】:

    【解决方案2】:

    您正在尝试删除不是来自实体接口的对象。您需要直接从实体(例如使用查询)实例化此 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多