Christophe De Troyer
Christophe De Troyer

Reputation: 2922

Context does not update

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

Upvotes: 1

Views: 1286

Answers (1)

StanK
StanK

Reputation: 4770

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;

Upvotes: 2

Related Questions