我听说过.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
请记住,我是这个框架的新手,感谢您的帮助
答案 0 :(得分:1)
重建我的项目后,抛出的异常消失了。我认为实体模型中的某些东西搞砸了,这引起了异常,但在重建我的项目之后,它“固定”了自己。