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 8283923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:54:56+00:00 2026-06-08T10:54:56+00:00

I’ve been reading and following the book Pro ASP.Net MVC3 Framework Third Edition by

  • 0

I’ve been reading and following the book “Pro ASP.Net MVC3 Framework Third Edition” by Freeman A. and Sanderson S.

I’ve reached the part where they tell me how to update entities. Everything works fine, except the update.

I’ve got a page to edit items in a store. Pretty basic. When I change values in the page, and submit them they reach the implementation of my repository they way they should, with the new values.

Now, the book shows me the following method in my repository (SaveProduct):

namespace SportsStore.Domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
         private EFDbContext context = new EFDbContext();
    public IQueryable<Product> Products
    {
        get { return context.Products; }
    }
    public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        context.SaveChanges();
    }
}
}

This method works, except it does not save my product. I’m not fully on top of the vagueness of MVC3, so I’m stuck. The product arrives as parameter in the SaveProduct method, with the proper values, and should be saved, yet the context is not updated.

Is this something that got updated in MVC3? (Is the book a little outdated?) Because I could code it with if’s to check all the values separately, but I can’t help feeling that that cancels the power of MVC3/Entity Framework.

Could anyone help me out? Thanks a lot! 🙂 In the meanwhile I’ll be debugging and looking for any clue’s!

Edit:

When I catch the integer that SaveChanges() returns, it seems to be zero. So I guess the entity with the new values is not “updated” to the entity framework.

Edit 2:

I’ve found the problem (already). Seems I was too quick asking questions, for which I apologize. Here is what I did:

I added the following code to the method SaveProduct:

    public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
            var productInDb = context.Products.FirstOrDefault(x => x.ProductID == product.ProductID);
            context.Entry(productInDb).CurrentValues.SetValues(product);
        }
        context.SaveChanges();
    }

I guess this is one proper way of doing so? Sorry for the useless question 🙂

  • 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-08T10:54:58+00:00Added an answer on June 8, 2026 at 10:54 am

    The issue is that the entity is not being tracked by your EFDbContext, so when you call context.SaveChanges();, it does not know that it needs to update it.

    So, you need to make sure that the context knows to track the entity’s status.

    There are basically two ways of doing this – either the way you list in your 2nd edit, or you can manually ‘attach’ the entity to the context and mark it as modified, as below:

    context.Products.Attach(product);
    context.Entry(product).State = EntityState.Modified;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.