实体框架:如果没有值更改,则取消属性更改 [英] Entity Framework: Cancel a property change if no change in value

查看:110
本文介绍了实体框架:如果没有值更改,则取消属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体对象上设置属性时,即使该值与之前的值完全相同,它仍将值保存到数据库。有没有办法阻止这个?

When setting a property on an entity object, it is saving the value to the database even if the value is exactly the same as it was before. Is there anyway to prevent this?

示例:

如果我加载Movie对象,标题是 A,如果我再次将标题设置为A,并且SaveChanges()我希望在SqlProfiler中不会看到UPDATE语句,但我是。有没有停止这个?

If I load a Movie object and the Title is "A", if I set the Title to "A" again and SaveChanges() I was hoping that I wouldn't see the UPDATE statement in SqlProfiler but I am. Is there anyway to stop this?

推荐答案

是的,你可以改变这一点。然而,在当前版本的实体框架中这样做并不是微不足道的。将来会变得更容易。

Yes, you can change this. Doing so isn't trivial, however, in the current version of the Entity Framework. It will become easier in the future.

您看到此行为的原因是因为实体模型的默认代码生成。这是一个代表性的例子:

The reason you're seeing this behavior is because of the default code generation for the entity model. Here is a representative example:

public global::System.Guid Id
{
    get
    {
        return this._Id;
    }
    set
    {
        // always!
        this.OnIdChanging(value);
        this.ReportPropertyChanging("Id");
        this._Id = global::System.Data.Objects.DataClasses
                               .StructuralObject.SetValidValue(value);
        this.ReportPropertyChanged("Id");
        this.OnIdChanged();
    }
}
private global::System.Guid _Id;
partial void OnIdChanging(global::System.Guid value);
partial void OnIdChanged();

此默认代码生成是合理的,因为实体框架不知道您打算的语义使用这些值。属性中的类型可能或可能不可比较,即使是这样,框架也不能知道您打算在所有情况下如何使用引用平等与值平等。对于像十进制的某些值类型,它很清楚,但在一般意义上说并不明显。

This default code generation is reasonable, because the Entity Framework doesn't know the semantics of how you intend to use the values. The types in the property may or may not be comparable, and even if they are, the framework can't know how you intend to use reference equality versus value equality in all cases. For certain value types like decimal, it's pretty clear, but in a general sense it's not obvious.

另一方面,你知道你的代码,并且可以自定义一些。麻烦的是,这是生成代码,所以你不能进去编辑它。您需要接管代码生成,或者使其不必要。所以我们来看看这三个选项。

You, on the other hand, know your code, and can customize this some. The trouble is that this is generated code, so you can't just go in and edit it. You need to either take over the code generation, or make it unnecessary. So let's look at the three options.

这里的基本方法是创建执行代码的T4模板,以及从实体框架生成的默认代码。 这是一个例子。这种方法的一个优点是,实体框架将在下一个版本中移至T4版本,因此您的模板可能在未来版本中运行良好

The essential approach here is to create a T4 template which does the code behind, and that the default code generation from the Entity Framework. Here is one example. One advantage of this approach is that the Entity Framework will be moving to T4 generation in the next version, so your template will probably work well in future versions.

第二种方法是完全消除热电联产,通过IPOCO手动进行变更跟踪支持。而不是改变代码的生成方式,使用这种方法,您根本不执行任何代码生成,而是通过实现多个接口向实体框架提供变更跟踪支持。

The second approach would be to eliminate cogeneration altogether, and do your change tracking support manually, via IPOCO. Instead of changing how the code is generated, with this approach you don't do any code generation at all, and instead provide change tracking support to the Entity Framework by implementing several interfaces. See the linked post for more detail.

另一种选择是以实体框架的方式生活这是暂时的,等到下一个版本才能得到你所期望的行为。 实体框架的下一个版本默认使用T4,因此自定义代码一代将很容易。

Another option is to live with the Entity Framework the way it is for the time being, and wait until the next release to get the behavior you desire. The next version of the Entity Framework will use T4 by default, so customizing the code generation will be very easy.

这篇关于实体框架:如果没有值更改,则取消属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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