我如何知道自我跟踪实体何时发生更改?

发布于 2024-11-20 00:19:17 字数 599 浏览 12 评论 0原文

我一直在使用实体框架+自我跟踪实体,并遇到了一个问题:

有没有办法确定实体何时被更改?

例如:如果您有一个实体User 有两个字段:名称和密码,您可以知道 User 实例是否已更改:

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

我的问题是当 User 具有 Person 时>,并且此人有一个电子邮件字段。我希望如果电子邮件字段发生更改,则相应的用户也会更改。

我一直在尝试使用以下方法: .StartTrackingAll(); 但这不适用于导航属性(或者也许我做错了什么)。有关此问题的一些帮助可以在此处找到。

请记住,自我跟踪实体是通过 T4 模板自动生成的,因此无法修改类。

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 found here.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-11-27 00:19:17

首先,当想要知道所谓的对象图中的任何实体是否已更改时,您可以递归遍历可跟踪集合中包含的所有实体或根实体(在您的情况下为用户)的一对一导航属性。这样你就可以知道根实体内的某个人是否发生了变化。这实际上是我检查复杂对象图以了解任何所包含实体中的任何更改的方式。但也用于检查这些实体中是否有任何一个存在严重的验证错误(因此用户还无法保留它们)。

请记住,自我跟踪实体是通过 T4 模板自动生成的,因此无法修改类。

不正确。首先你可以修改T4模板来生成更多(复杂)的代码来实现你想要的东西。其次,它生成可以使用自定义(非生成)代码轻松扩展的部分类。

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.

白馒头 2024-11-27 00:19:17

如果您更改 Person 实例中的电子邮件,则只有该实例才会正确标记为已修改。这是绝对正确的行为。你期望什么?您是否期望相关实体中的属性更改会将更改后的状态传播到关系?这将使 STE 完全无用,因为对实体图的任何单个更改都会使图中的所有实体都被修改,并且每次修改都会导致数据库的额外往返。

如果您想在更改电子邮件时将User设置为已修改,只需创建一些方法或处理一些事件并调用person.User.MarkAsModified()

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文