【发布时间】:2018-05-03 13:59:30
【问题描述】:
我刚刚在我的项目中添加了一个实体对象。当我去 savechanges 时,它告诉我它没有找到。
这是一个例子:
还有代码:
[Authorize]
[SessionExpireFilter]
public ActionResult Delink()
{
var control = Logging.StartLog();
control.ClassName = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
if (CurrentCustomerSession.Current.AccountGuid == Guid.Empty)
{
Logging.WriteLog(control, "Redirecting to AddCustomer:Customer since CurrentAccountGuid == null");
return RedirectToAction("AddCustomer", "Customer");
}
var username = System.Web.HttpContext.Current.User.Identity.Name.ToLower();
var entities = new SuburbanPortalEntities();
var qry = (from x in entities.UsersAccountLinks
where x.AccountId == CurrentCustomerSession.Current.AccountGuid && x.aspnet_Users.LoweredUserName == username
select x).FirstOrDefault();
if(qry == null)
return View("Error");
qry.AccountId = Guid.Empty;
entities.SaveChanges();
CurrentCustomerSession.Current.AccountGuid = Guid.Empty;
CurrentCustomerSession.Current.AccountNumber = string.Empty;
CurrentCustomerSession.Current.Branch = string.Empty;
//return View("");
return RedirectToAction("AccountScreen", "Customer");
}
catch (Exception ex)
{
Logging.WriteLog(control, string.Format("exception: {0}", ex.Message));
Logging.WriteException(control, ex);
return View("Error");
}
finally
{
Logging.WriteLog(control, "End Delink");
Logging.FlushLog(control);
}
}
我现在不确定要检查什么。我已删除 SuburbanPortalEntities 并重新读取它,但没有任何成功。
有什么建议吗?
【问题讨论】:
-
“当我去 savechanges”,这是什么意思?您的意思是“当我使用调用 SaveChanges 编译我的代码时?”。或者你说的是导航?检查错误列表并发布确切的编译错误。
SuburbanPortalEntities的班级是否没有错误? -
你在保存什么,查询返回 null 还是不喜欢空的 GUID 集?
-
我无法编译,这是编译时错误。
标签: c# asp.net-mvc entity-framework-6