使用EntityFramework&删除MVC3中的表中的记录时出错LINQ [英] Error While Deleteing a record in a table in MVC3 using EntityFramework & Linq

查看:52
本文介绍了使用EntityFramework&删除MVC3中的表中的记录时出错LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii to all,



当我想要删除记录时,我发现以下错误

错误: - 存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目



这是我的控制器代码: -

public ActionResult关于()

{

//员工模特=新员工();

返回查看();

}

[HttpPost]

公共ActionResult关于(员工模型)

{

使用(personalEntities db = new personalEntities())

{

checkeddetail obj = new checkeddetail();

var delete =(来自db.checkeddetails中的s其中model .searchByName == s.textname select s.empid).FirstOrDefault();

obj.empid = delete;

db.checkeddetails.Attach(obj);

db.checkeddetails.DeleteObject(obj);

db.SaveChanges();

}

返回View() ;

}
请指导我如何克服以下错误



谢谢

Ramu

解决方案

可能是因为您的个人身份证件(密钥)丢失了。通过设置断点来检查值。

有关更多信息,请查看 this [ ^ ] thread

编辑:

  public  ActionResult关于()
{
// 员工模型=新员工();
return View();
}
[HttpPost]
public ActionResult关于(员工模型)
{
使用(personalEntities db = new personalEntities())
{
var delete =( from s in db.checkeddetails where model.searchByName == s.textname select s.empid).FirstOrDefault();
db.checkeddetails.DeleteObject(delete);
db.SaveChanges();
}
return 查看();
}


Hii to all,

when i want to delete a record iam finding the following Error
ERROR:-Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries

This Is My Controller Code:-
public ActionResult About()
{
//Employee model = new Employee();
return View();
}
[HttpPost]
public ActionResult About(Employee model)
{
using (personalEntities db = new personalEntities())
{
checkeddetail obj = new checkeddetail();
var delete = (from s in db.checkeddetails where model.searchByName == s.textname select s.empid).FirstOrDefault();
obj.empid = delete;
db.checkeddetails.Attach(obj);
db.checkeddetails.DeleteObject(obj);
db.SaveChanges();
}
return View();
}
Please guide me how should i overcome the following Error

Thanks
Ramu

解决方案

It may be because your personal identity ID(key) is missing. Check the value by putting a break point.
For more information, check this[^] thread
EDIT:

public ActionResult About()
{
    //Employee model = new Employee();
    return View();
}
[HttpPost]
public ActionResult About(Employee model)
{
   using (personalEntities db = new personalEntities())
   {
      var delete = (from s in db.checkeddetails where model.searchByName == s.textname select s.empid).FirstOrDefault();
      db.checkeddetails.DeleteObject(delete);
      db.SaveChanges();
   }
   return View();
}


这篇关于使用EntityFramework&删除MVC3中的表中的记录时出错LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆