FormDataEntities en.SaveChanges() 抛出异常
我听说了 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重建我的项目后,引发的异常消失了。我认为实体模型中的某些内容弄乱了,这导致了异常,但在重建我的项目后,它自行“修复”了。
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.