EntityFramework,工作单元 - 跟踪自定义数据的更改并通过 WebService 发送它

发布于 2024-11-18 18:43:20 字数 295 浏览 2 评论 0原文

我们在 EntityFramework 中实现了工作单元,因此当我们使用 ObjectContext 并对实体进行任何更改时,它都会被跟踪,然后在 SaveChanges 上,它都会反映在底层数据库中。

但是,如果我想跟踪自定义类的更改,以便跟踪每个修改并通过 Web 服务调用发送,该怎么办?

我有网络服务,它为我提供一些数据,这些数据显示在数据网格中,然后可以进行修改。我想跟踪所有更改,然后能够通过网络服务仅发回已修改的数据。有没有像 EntityFramework 或 POCO 之类的解决方案?或者我必须为其实现我自己的工作单元模式?

We have Unit of Work implemented in EntityFramework, so when we use ObjectContext and make any changes to the Entity it is tracked and then on SaveChanges it is all reflected in underlying database.

But what if I want to track changes for my custom class, so every modifications are tracked down and sent through webservice call ?

I have webservice which provides me some data, that data is displayed in datagrid and then may be modified. I want to track all the changes down and then be able to send back through webservice the data only that have been modified. Is there any solution for that like EntityFramework or POCO or whatever ? Or I have to implement my own Unit of Work pattern for it ?

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

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

发布评论

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

评论(2

情绪失控 2024-11-25 18:43:20

仅当实体附加到上下文时,更改跟踪才有效。有一种特殊类型的实体,称为自我跟踪实体 当通过 Web 服务公开时,它能够跟踪客户端的更改,但这些类仍然是您的主要实体(而不是自定义对象),并且它们将其跟踪状态直接应用于上下文。

您所描述的与工作单元模式无关。您正在寻找能够仅将差异传递回服务的更改集模式。此类课程的实现完全取决于您。 .NET 不提供它们。 .NET 提供了提到的更改集模式的两种实现

  • EF
  • DataSet 和相关类的

自跟踪实体这两种实现都默认传输所有数据(此外,至少 DataSet 默认情况下在消息中同时具有旧状态和新状态)。数据集和 STE 都有相同的局限性 - 它们互操作性非常差。

Change tracking works only when entity is attached to the context. There is special type of entities called Self tracking entities which is able to track changes on the client side when exposed with web service but these classes are still your primary entities (not custom objects) and they apply their tracked state directly to the context.

What you describe has nothing to do with unit-of-work pattern. You are looking for change set pattern which is able to pass only differences back to the service. Implementation of such classes is completely up to you. .NET doesn't provide them. .NET offers two implementations of change set pattern

  • mentioned Self tracking entities for EF
  • DataSet and related classes

Both these implementations transfer by default all data (moreover at least DataSets have by default both old and new state in the message). Both data sets and STEs share same limitations - they are very badly interoperable.

我家小可爱 2024-11-25 18:43:20

由于多种原因,不应将属性级别的更改跟踪留给 WCF 调用的客户端。如果您使用 DTO(数据传输对象)模式,您应该能够使各个对象保持足够小,以避免通过网络发送整个更改的对象而产生任何重大开销。然后,在服务器端,从数据库中加载对象的当前版本,设置 DTO 提供的值,并让实体框架跟踪更改的属性。

public SavePerson(Person person)
{
    using(var context = _contextFactory.Get())
    {
        var persistentPerson = context.People.Single(p => p.PersonId == person.PersonId);
        persistendPerson.FirstName = person.FirstName;
        /// etc. (This could be done with a tool like AutoMapper)
        context.SaveChanges();
    }
}

如果您要在客户端更改多个对象,并且想要跟踪用户更改了哪些对象,则可以让客户端负责跟踪已更改的对象并仅将这些对象发送到 Web批量服务。在那里,您可以应用相同的模式并等待 SaveChanges,直到所有对象都已更新。

希望这有帮助。

Change tracking at the property level should not be left to the client of a WCF call, for a variety of reasons. If you use a DTO (Data-Transfer Object) pattern, you should be able to keep your individual objects small enough to avoid having any significant overhead from sending the entire changed object across the wire. Then, on the server side, you load the current version of the object out of your database, set the values provided by the DTO, and let Entity Framework track the changed properties.

public SavePerson(Person person)
{
    using(var context = _contextFactory.Get())
    {
        var persistentPerson = context.People.Single(p => p.PersonId == person.PersonId);
        persistendPerson.FirstName = person.FirstName;
        /// etc. (This could be done with a tool like AutoMapper)
        context.SaveChanges();
    }
}

If you're changing multiple objects on the client side, and you want to keep track of which ones the user has changed, you could have the client be responsible for keeping track of the objects that get changed and send only those objects to the web service in bulk. There, you can apply the same pattern and wait to SaveChanges until all of the objects have been updated.

Hopefully this helps.

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