如何在实体框架中从我的数据库中删除我的问题 [英] How Do I Solve My Problem In Deleting From My Database In Entity Framework

查看:74
本文介绍了如何在实体框架中从我的数据库中删除我的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2012和实体框架开发内容管理系统。

我运行这部分:

im working on a content management system with visual studio 2012 and entity framework.
as i run this part:

protected void grdResults_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    int w = int.Parse(grdResults.Rows[e.RowIndex].Cells[0].Text);
    NewsDbModel.NewsDbEntities db = new NewsDbModel.NewsDbEntities();
   
    var x = (from NewsTable in db.Tables where NewsTable.Id == w select NewsTable);
    db.DeleteObject(x); 
      
    db.SaveChanges();

}



i面临此错误:

无法删除对象,因为在ObjectStateManager中找不到该对象。



请帮我解决这个问题。

非常感谢


i face this error:
"The object cannot be deleted because it was not found in the ObjectStateManager."

pls help me in solving this problem.
thanks alot

推荐答案

好吧,你试图删除两次相同的对象。这应该是一个问题。

还要为你的 x 变量添加一个检查,以便它实际上包含一个值。



Well, you try to delete the same object twice. That should be an issue.
Also add a check for your x variable so it actually contains a value.

protected void grdResults_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    int w = int.Parse(grdResults.Rows[e.RowIndex].Cells[0].Text);
    NewsDbModel.NewsDbEntities db = new NewsDbModel.NewsDbEntities();

    var x = (from NewsTable in db.Tables where NewsTable.Id == w select NewsTable);
    if (x != null)    // Set a break point here and debug.
        db.DeleteObject(x);
    db.DeleteObject(x);

    db.SaveChanges();

}


这篇关于如何在实体框架中从我的数据库中删除我的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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