0

好的,所以我正在尝试从表单更新我的数据库中的信息。但是当我调用 db.SaveShanges() 时,我得到了 KeyNotFoundException。我正在使用 MVC3 和 EF 4.1。我使用 Controller->Service->Repositry->EF 设计模式。

调用栈:

   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at System.Data.Objects.EntityEntry.UpdateComplexObjectSnapshot(StateManagerMemberMetadata member, Object userObject, Int32 ordinal, Object currentValue)
   at System.Data.Objects.EntityEntry.DetectChangesInProperties(Boolean detectOnlyComplexProperties)
   at System.Data.Objects.ObjectStateManager.DetectChangesInScalarAndComplexProperties(IList`1 entries)
   at System.Data.Objects.ObjectStateManager.DetectChanges()
   at System.Data.Objects.ObjectContext.DetectChanges()
   at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
   at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
   at System.Data.Entity.Internal.InternalContext.GetStateEntries()
   at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
   at System.Data.Entity.DbContext.GetValidationErrors()
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()

我的存储库 db 是一个带有 DbSet 的 dbContext,用于我正在使用的模型。

public bool Save()
{
    try
    {
        db.SaveChanges();
    }
    catch(Exception e)
    {
        return false;
    }
    return true;
}

调用存储库的我的服务

public bool UpdateUserInformationFromSettingsModel(UserSettingsModel model)
{

    int userInfoID = _profile.GetUserInformationID(model.userName);
    UserInformation userInfo = _repository.Get(userInfoID);
    if (userInfo == null)
    {
        return false;
    }
    userInfo.firstName = model.firstName;
    userInfo.lastName = model.lastName;
    userInfo.phone = model.phone;
    userInfo.invoiceReciever = model.invoiceReciever;
    userInfo.invoiceAddress = model.invoiceAddress;
    userInfo.address = model.address;
    userInfo.preferedLanguage = model.preferedLanguage;
    bool saveSuccess = _repository.Save();
    if(!saveSuccess)
    {
        return false;
    }

    return true;
}

我的控制器

        [HttpPost]
    public ActionResult Edit(UserSettingsModel model)
    {
        if(ModelState.IsValid)
        {
            if (_userService.UpdateUserInformationFromSettingsModel(model))
            {
                return RedirectToAction("Settings");
            }
            ModelState.AddModelError("", GuiText.editSettingsError);
        }
        return View(model);
    }

用户信息模型

public class UserInformation
{
    public int ID { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string phone { get; set; }
    public string invoiceSendOption { get; set; }
    public string invoiceReciever { get; set; }
    public Address invoiceAddress { get; set; }
    public Address address { get; set; }
    public string preferedLanguage { get; set; }
    public string confirmCode { get; set; }
    public UserSiteSettings siteSettings { get; set; }


    public UserInformation()
    {
        firstName = "";
        lastName = "";
        phone = "";
        invoiceSendOption = "";
        invoiceReciever = "";
        invoiceAddress = new Address();
        address = new Address();
        preferedLanguage = "";
        confirmCode = "";
        siteSettings = new UserSiteSettings();
    }

用户设置模型

    public class UserSettingsModel
{
    public string userName { get; set; }
    [Display(Name = "name", ResourceType=typeof(GuiText))]
    public string firstName { get; set; }
    public string lastName { get; set; }
    [Display(Name = "phone", ResourceType = typeof(GuiText))]
    public string phone { get; set; }
    [Display(Name = "invoiceInfo", ResourceType = typeof(GuiText))]
    public string invoiceReciever { get; set; }
    public Address invoiceAddress { get; set; }
    [Display(Name = "address", ResourceType = typeof(GuiText))]
    public Address address { get; set; }
    [Display(Name = "prefLang", ResourceType = typeof(GuiText))]
    public string preferedLanguage { get; set; }
    public List<SelectListItem> preferedLanguageList { get; set; }

我已经检查了调试器中的变量,一切似乎都很好。

我正在使用 MySql 连接器 v6.5.4。

所以有人知道问题可能是什么吗?

4

1 回答 1

1

经过几天的挫折和搜索,我发现了问题。我在我的 UserInformation 和 Address 类中使用了自定义的 equals 方法。这导致了问题。现在我已经删除了它并改变了我的单元测试以另一种方式工作。

于 2012-08-29T12:02:09.930 回答