在EF 4中申请税率 [英] ApplyCurrentValues in EF 4

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

问题描述

我刚刚在VS 2010 RC中玩EF 4,刚刚发现当Property属于bool类型且新值为false时,ApplyCurrentValues不工作!


,当新值为true时,它将起作用。


我不知道这是否是一个错误,或者我错过了一些,但我只是在一个非常丑陋的工作:

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?

推荐答案

您附带了一个新的产品所有布尔属性的值(false)。然后,将这些值中的一个设置为false。没有惊喜,它不更新;你没有真的改变它!在我看来,您可以通过删除一些代码来解决这个问题:

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中申请税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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