FormDataEntities en.SaveChanges() 抛出异常

发布于 2024-08-10 20:56:14 字数 1542 浏览 9 评论 0原文

我听说了 .net 的新实体框架,并决定修改我的代码以使用该框架,但是当我尝试从表中添加和删除记录时,会引发异常。我是这个框架的新手,所以我不知道如何解决这个问题或是什么导致了异常。这是我的代码(c#):

try
{
    string key = Request.QueryString["account"].ToString();
    FormDataEntities en = new FormDataEntities();

    DbUser usr = new DbUser();
    usr.uid = (from b in en.DbUnactivatedUserSet where b.active == key select b.uid).FirstOrDefault();
    usr.pwd = (from b in en.DbUnactivatedUserSet where b.active == key select b.pwd).FirstOrDefault();
    usr.firstname = (from b in en.DbUnactivatedUserSet where b.active == key select b.firstname).FirstOrDefault();
    usr.lastname = (from b in en.DbUnactivatedUserSet where b.active == key select b.lastname).FirstOrDefault();

    en.AddToDbUserSet(usr);

    DbUnactivatedUser un = (from b in en.DbUnactivatedUserSet where b.active == key select b).FirstOrDefault();

    en.DeleteObject(un);
    en.SaveChanges();

    Label1.Text = "message";
}
catch (Exception ex)
{
     Label1.Text = ex.StackTrace;
}

这是我的 StackTrace:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at  System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
at System.Data.Objects.ObjectContext.SaveChanges()
at ASP.pages_activate_aspx.Page_Load(Object sender, EventArgs e)
in c:\inetpub\My Web Sites\mysite\Pages\Activate.aspx:line 26

请记住,我是这个框架的新手,感谢您的帮助

I heard about the new entity framework for .net, and decided to modify my code to use this framework, but when I try to add and delete records from the table, an Exception is thrown. I am NEW to this framework, so I can't figure out how to fix this or what is causing the exception. Here is my Code(c#):

try
{
    string key = Request.QueryString["account"].ToString();
    FormDataEntities en = new FormDataEntities();

    DbUser usr = new DbUser();
    usr.uid = (from b in en.DbUnactivatedUserSet where b.active == key select b.uid).FirstOrDefault();
    usr.pwd = (from b in en.DbUnactivatedUserSet where b.active == key select b.pwd).FirstOrDefault();
    usr.firstname = (from b in en.DbUnactivatedUserSet where b.active == key select b.firstname).FirstOrDefault();
    usr.lastname = (from b in en.DbUnactivatedUserSet where b.active == key select b.lastname).FirstOrDefault();

    en.AddToDbUserSet(usr);

    DbUnactivatedUser un = (from b in en.DbUnactivatedUserSet where b.active == key select b).FirstOrDefault();

    en.DeleteObject(un);
    en.SaveChanges();

    Label1.Text = "message";
}
catch (Exception ex)
{
     Label1.Text = ex.StackTrace;
}

and here is my StackTrace:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at  System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
at System.Data.Objects.ObjectContext.SaveChanges()
at ASP.pages_activate_aspx.Page_Load(Object sender, EventArgs e)
in c:\inetpub\My Web Sites\mysite\Pages\Activate.aspx:line 26

Please keep in mind that I am new to this framework, and thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2024-08-17 20:56:14

重建我的项目后,引发的异常消失了。我认为实体模型中的某些内容弄乱了,这导致了异常,但在重建我的项目后,它自行“修复”了。

After rebuilding my project, the exception that had been thrown disappeared. I think something in the entity model got messed up, and this caused an exception, but after rebuilding my project, it 'fixed' itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文