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

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

4

1 回答 1

1

重建我的项目后,抛出的异常消失了。我认为实体模型中的某些东西搞砸了,这导致了异常,但是在重建我的项目后,它“修复”了自己。

于 2009-11-08T03:14:21.550 回答