Only update actually updated fields in Entity Framework
Quite a common use case, it seems, is when re-populating an object from a form is to go
myobj.Name = "textbox value";
myobj.Content = "textbox content";
But, name may not have changed, it may only be a change to the content textbox.
The problem is that entity framework treats Name as changed just开发者_如何学Python because I've set it's value, regardless of whether I've set exactly the same value or not.
Ideally, I would like EF to only mark things as changed if they genuinely have changed. Is this possible?
What version of EF are you using?
Try this - go to definition of Name property in the entity, and see if it has a check in it's setter like:
set
{
if (Name != value)
...
}
If it doesn't, alter the t4 templates (if you're using EF 4.0) and add manually.
Otherwise, I don't think there's a cheap way of 'unchanging' the changed property.
Edit:
To create T4 from existing model, right click your model, choose Add code generation items
, and choose EntityObject generator
. This will create a tt file that you can run by issuing a save command (you'll get a prompt if you want to allow it to run). When saved, it will generate a file that's exactly the same as the file generated by edmx model. Now, you just need to locate the part where it generates a property set
. The tt can be intimidating at first look, but it really pays off learning it... there's an extension for coloring the text of t4 - you can find it in extension manager and it's free.
精彩评论