开发者

how can i know when a self-tracking entity has been changed?

I have been working with the Entity Framework + Self-Tracking entities, and came out with a problem:

Is there any way to determine when an entity has been changed??

For example: If you have an entity User with two fields: Name and Password, you can know if an User instance has been changed making:

<user>.ChangeTracker.State != ObjectState.Unchanged;

My problem is when the User has a Person, and the person has a field Email. I want that if the email field is changed, then the corresponding User is changed too.

I have been trying with methods such as: <user>.StartTrackingAll(); but this does not work with navigation properties (or maybe i am doing something wrong). Some help about this can be开发者_如何学Python found here.

Remember that the Self tracking entities are autogenerated via T4 templates, so the clases can't be modified.


First when wanting to know if any entity in a so-called object graph has changed you can recurse through all entities contained in trackable collections or one-to-one navigation properties of a root entity (user in your case). This way you can know if a person inside the root entity has changed. This is actually how I check complex object graphs for any changes in any of the contained entities. But also for checking out if any of these entities have critical validation errors (so the user can't persist them yet).

Remember that the Self tracking entities are autogenerated via T4 templates, so the clases can't be modified.

Not true. First of all you can modify the T4 template to generate more (complex) code to achieve the things you want. And second, it generates partial classes which can easily be extended with custom (non-generated) code.


If you change the email in the Person instance only that instance is correctly marked as modified. That is absolutely correct behaviour. What do you expect? Do you expect that change to property in related entity will propagate changed state to relations? That would make STEs completely useless because any single change to entity graph would make all entities in the graph modified and each this modification causes additional roundtrip to the database.

If you want to set User as modified when you are changing email simply create some method or handle some event and call person.User.MarkAsModified()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜