在EF 4 ApplyCurrentValues [英] ApplyCurrentValues in EF 4

查看:378
本文介绍了在EF 4 ApplyCurrentValues的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用EF 4打在VS 2010 RC版,只是发现ApplyCurrentValues​​不工作时,该物业为BOOL类型和新价值是假的!!! ???
和它的作品的时候,新的价值是真实的。
我不知道这是否是一个错误还是我失去了一些东西,但我只是一个非常丑陋的变通工作:

 公共无效的UpdateProduct(产品updatedProduct)
    {
        使用(模型)
        {
            model.Products.Attach(新产品{产品ID = updatedProduct.ProductID});
            model.Products.ApplyCurrentValues​​(updatedProduct);
            产品originalProduct = model.Products.Single(P => p.ProductID == updatedProduct.ProductID);
            originalProduct.Discontinued = updatedProduct.Discontinued;
            model.SaveChanges();

        }

    }
 

任何想法或更好的变通?

解决方案

您连接一个新的产品与所有的布尔属性(假)默认值

。然后,设置这些值错误之一。难怪它不会更新;你还没有真正改变了它!在我看来,你可以通过删除你的一些code解决这个问题:

 公共无效的UpdateProduct(产品updatedProduct)
{
    使用(模型)
    {
        产品originalProduct = model.Products.Single(P => p.ProductID == updatedProduct.ProductID);
        model.Products.ApplyCurrentValues​​(updatedProduct);
        model.SaveChanges();
    }
}
 

即使你不喜欢这样,尝试一下,看看是否可行。

现在在我看来,你正试图避免在首位加载该产品。但是这样做打破了你的code。所以,虽然我的问题试图优化的更新(您加载一个记录在这里,和更新发生了很多不经常然后选择),让我们同意开始与一些东西,工作。

如果这个工程,它告诉你你所需要的,如果你坚持做就避免加载产品更新:你需要的标记所有属性修改

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?

解决方案

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.

这篇关于在EF 4 ApplyCurrentValues的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆