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

  • Home
  • SEARCH
  • 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 3722686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:59:43+00:00 2026-05-19T05:59:43+00:00

I have 2 projects – a class library containing an EDM Entity Framework model

  • 0

I have 2 projects – a class library containing an EDM Entity Framework model and a seperate ASP.NET MVC project.

I’m having problems with how your suppose to edit and save changes to an entity using MVC. In my controller I have:

public class UserController : Controller
    {
        public ActionResult Edit(int id)
        {
            var rep = new UserRepository();

            var user = rep.GetById(id);

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(User user)
        {
            var rep = new UserRepository();

            rep.Update(user);

            return View(user);
        }
    }

My UserRepository has an Update method like this:

public void Update(User user)
{
     using (var context = new PDS_FMPEntities())
     {
         context.Users.Attach(testUser);
         context.ObjectStateManager.ChangeObjectState(testUser, EntityState.Modified);
         context.SaveChanges();
     }
}

Now, when I click ‘Save’ on the edit user page, the parameter user only contains two values populated: Id, and FirstName. I take it that is due to the fact that I’m only displaying those two properties in the view.

My question is this, if I’m updating the user’s firstname, and then want to save it, what am I suppose to do about the other User properties which were not shown on the view, since they now contain 0 or NULL values in the user object?

I’ve been reading a lot about using stub entities, but I’m getting nowhere fast, in that none of the examples I’ve seen actually work. i.e. I keep getting EntityKey related exceptions.

Can someone point me to a good tutorial/example of how to update EF 4 entities using a repository class, called by an MVC front-end?

  • 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-05-19T05:59:44+00:00Added an answer on May 19, 2026 at 5:59 am

    OK, after some trial and error, I look to have found a solution. Here is my updated Update method in the UserRepository:

    public void Update(User user)
    {
        using (this.Context)
        {
            var tempUser = new User { usr_id = user.usr_id };
    
            this.Context.Users.Attach(tempUser);
            this.Context.ApplyCurrentValues("Users", user);
            this.Context.SaveChanges();
        }
    }
    

    Some of other examples I tried were very close to the above, but just missed the mark.

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

Sidebar

Related Questions

I have several projects and a website in a large asp.net solution. In some
I have two projects, a Cocoa iPhone application and a static library which it
I have two projects, the DLL project which has all my logic and data
I have projects A and B. Both projects use component C (a graphical library
I have 3 tables: projects, skills and project_skills. In projects table i hold project's
I have two projects configured identically for log4net. One project logs fine; however, the
I have multiple projects in one solution. Project A (the starting project) starts Project
I am getting a web host and i have projects with teammats. I thought
Some projects have properties have miscellaneous settings such as: AllowPayments, ShowSideBar, SectionTitle. Really things
I have multiple projects in a couple of different workspaces. However, it seems like

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.