Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:13:34+00:00 2026-06-11T01:13:34+00:00

Okey so i’m trying to update information in my database from a form. But

  • 0

Okey so i’m trying to update information in my database from a form.
But when i call db.SaveShanges() i get KeyNotFoundException.
I’m using MVC3 and EF 4.1.
I use Controller->Service->Repositry->EF design pattern.

The callstack:

   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()

My repositrory db is a dbContext with a DbSet for the model i’m using.

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

My Service that calls the repository

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;
}

My controller

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

The UserInformation 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();
    }

The UserSettingsModel

    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; }

I have checked the variables in debugger and it all seems to be ok.

I’m using MySql connector v6.5.4.

So anyone got any ideas what the problem might be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:13:36+00:00Added an answer on June 11, 2026 at 1:13 am

    After some days of frustration and searching i found the problem.
    I used a custom made equals method in my UserInformation and Address class. That caused the problems. Now i have removed it and changed my Unit Tests to work in another way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to download multiple files using IntentService. The IntentService donwloads them okey as
I'm using grep to extract parts from a file but I'm having trouble making
Okey, I've done all the business, followed all the steps, but still can't get
I am using HTML5. I want to Call API with AJAX. but, not success
I'm using JQuery UI Dialog. In this form, I validate something.I call this function;
I am wondering about the performance of connecting to the database. Is it okey
Okey, so the question has probably been asked before, but I haven't found an
Okey, I have two strings containing numbers like shown here using debugger: String 1
okey so here is my structure. one.php two.php three.php one.php includes both two.php and
Okey, here it goes. I have a block of code that repeats it self

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.