Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
240 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...