开发者

Does EF update objects just because a property changes or only if it has a different value?

开发者 https://www.devze.com 2023-01-14 07:42 出处:网络
I have an import, from flat file to an EDM against SQL Server, where very few records actually change (running it the umpteenth time with the same import file), but nonetheless the SaveChanges call to

I have an import, from flat file to an EDM against SQL Server, where very few records actually change (running it the umpteenth time with the same import file), but nonetheless the SaveChanges call to update 500 records takes quite long. I'm guessing this is because I set each property on the entity object, and EF then开发者_StackOverflow uses ReportPropertyChanged to signal that the record must be updated in the DB.

Do I actually have to compare all import recort property values to entity property values, and only assign to the entity property when they differ, to avoid these lengthy 'do-nothing' updates?

EDIT: The generated code for the setter for the Updated property looks like below. I can see the assignment is unconditional, which is why I'm asking if the actual generation of an UPDATE statement to the DB is conditional or not.

set
{
    OnUpdatedChanging(value);
    ReportPropertyChanging("Updated");
    _Updated = StructuralObject.SetValidValue(value);
    ReportPropertyChanged("Updated");
    OnUpdatedChanged();
}


Do you use generated entity code? It already checks if value is same:

[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 Id
{
    get
    {
        return _Id;
    }
    set
    {
        if (_Id != value)
        {
            OnIdChanging(value);
            ReportPropertyChanging("Id");
            _Id = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("Id");
            OnIdChanged();
        }
    }
}
private global::System.Int32 _Id;
partial void OnIdChanging(global::System.Int32 value);
partial void OnIdChanged();
0

精彩评论

暂无评论...
验证码 换一张
取 消