6

I just was playing with EF 4 in VS 2010 RC and just found that ApplyCurrentValues dont work when the Property is of type bool and the newly value is false !!!???
and it works when the newly value is true .
I dont know if this is a bug or I'm missing something but I just work with a very ugly work around :

public void UpdateProduct(Product updatedProduct)
    {
        using (model)
        {
            model.Products.Attach(new Product { ProductID = updatedProduct.ProductID });
            model.Products.ApplyCurrentValues(updatedProduct);
            Product originalProduct = model.Products.Single(p => p.ProductID == updatedProduct.ProductID);
            originalProduct.Discontinued = updatedProduct.Discontinued;
            model.SaveChanges();

        }

    }

any idea or better work around?

John Smithers
  • 1,519
  • 12
  • 18
ali62b
  • 5,363
  • 4
  • 22
  • 25

2 Answers2

20

You attached a new Product with the default values for all bool properties (false). You then set one of those values false. No surprise it doesn't update; you haven't actually changed it! It seems to me you could solve this by removing some of your code:

public void UpdateProduct(Product updatedProduct)
{
    using (model)
    {
        Product originalProduct = model.Products.Single(p => p.ProductID == updatedProduct.ProductID);
        model.Products.ApplyCurrentValues(updatedProduct);
        model.SaveChanges();
    }
}

Even if you don't like this, try it and see if it works.

Now seems to me that you are trying to avoid loading the product in the first place. But doing so broke your code. So although I questions attempting to "optimize" an update (you are loading one record here, and updates happen a lot less often then selects), let's agree to start with something which works.

If this works, it tells you what you need to do if you insist on avoiding loading the product for update: you need to mark all properties as modified.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    I gave you another fix (last sentence of my answer). Did you try it before downvoting, or did you downvote without reading the whole answer? The point of the code is to demonstrate precisely what the issue was. – Craig Stuntz Feb 16 '10 at 18:03
  • It worked sorry for downvoting :( could you edit your answer so I can upvote ? thanks – ali62b Feb 17 '10 at 03:45
  • 2
    Fixed my issue http://stackoverflow.com/questions/2264313/entity-framwork-4-not-always-updating-boolean-property-using-applycurrentvalues as well. – Cephas Feb 17 '10 at 04:26
  • 1
    @ali62b, I don't care nearly so much about the downvote as I do that you didn't seem to try what I suggested, and, hence, still had your problem. If you've tried it and now have working code, that's great! – Craig Stuntz Feb 17 '10 at 13:55
  • 1
    @Craig, have you thought about a way to make this technique into a generic pattern? – John Kaster May 19 '11 at 20:50
  • @John, I don't understand: More generic than the one in Danny's blog (linked in answer)? – Craig Stuntz May 20 '11 at 02:11
  • @Craig, unless I misunderstand, that marks ALL properties as modified, so your SQL generates an update for all properties. I'm looking for a generic pattern based on what you show above where only changes are pushed. – John Kaster May 20 '11 at 17:31
  • @John, you're right. No, I haven't done anything else with this. However, if you look at Danny's earlier post, he shows how to update individual properties in the `ObjectStateManager` – Craig Stuntz May 20 '11 at 21:40
1

Modified:

public void UpdateProduct(Product updatedProduct)
{
    using (model)
    {
        model.Products.Single(p => p.ProductID == updatedProduct.ProductID);
        model.Products.ApplyCurrentValues(updatedProduct);
        model.SaveChanges();
    }
}
halfelf
  • 9,737
  • 13
  • 54
  • 63
PayCheck
  • 11
  • 1