简体   繁体   English

实体框架POCO长期变更跟踪

[英]Entity Framework POCO long-term change tracking

I'm using .NET entity framework 4.1 with code-first approach to effectively solve the following problem, here simplified. 我使用.NET实体框架4.1采用代码优先的方法来有效地解决以下问题,这里简化了。

  • There's a database table with tens of thousands of entries. 有一个包含数万个条目的数据库表。
  • Several users of my program need to be able to 我的程序的几个用户需要能够
    • View the (entire) table in a GridRow, which implied that the entire Table has to be downloaded. 查看GridRow中的(整个)表,这意味着必须下载整个表。
    • Modify values of any random row, changes are frequent but need not be persisted immediately. 修改任意随机行的值,更改频繁但不需要立即保留。 It's expected that different users will modify different rows, but this is not always true. 预计不同的用户将修改不同的行,但这并非总是如此。 Some loss of changes is permitted, as users will most likely update same rows to same values. 允许进行一些更改,因为用户很可能会将相同的行更新为相同的值。
    • On occasion add new rows. 有时添加新行。

Sounds simple enough. 听起来很简单。 My initial approach was to use a long-running DbContext instance. 我最初的方法是使用长时间运行的DbContext实例。 This one DbContext was supposed to track changes to the entities, so that when SaveChanges() is called, most of the legwork is done automatically. 这个DbContext应该跟踪实体的变化,这样当调用SaveChanges()时,大部分的工作都是自动完成的。 However many have pointed out that this is not an optimal solution in the long run , notably here . 然而,许多人指出,从长远来看 ,这不是最佳解决方案 ,特别是在这里 I'm still not sure if I understand the reasons, and I don't see what a unit-of-work is in my scenario either. 我还不确定我是否理解其中的原因,而且我也不知道我的方案中的工作单元是什么。 The user chooses herself when to persist changes, and let's say that client always wins for simplicity. 用户选择何时持续更改,并且假设客户总是为了简单而获胜。 It's also important to note that objects that have not been touched don't overwrite any data in the database. 同样重要的是要注意,未触摸的对象不会覆盖数据库中的任何数据。

Another approach would be to track changes manually or use objects that track changes for me, however I'm not too familiar with such techniques, and I would welcome a nudge in the right direction. 另一种方法是手动跟踪变化或使用跟踪变化的对象,但是我不太熟悉这些技术,我欢迎在正确的方向上轻推。

What's the correct way to solve this problem? 解决这个问题的正确方法是什么?

I understand that this question is a bit wishy-washy, but think of it as more fundamental. 我知道这个问题有点过于愚蠢,但认为它更为根本。 I lack fundamental understanding about how to solve this class of problems. 我对如何解决这类问题缺乏基本的了解。 It seems to me that long living DbContext is the right way, but knowledgeable people tell me otherwise, which leads me to confusion and imprecise questions. 在我看来,长期生活DbContext是正确的方式,但知识渊博的人告诉我,这导致我混淆和不精确的问题。

EDIT1 Another point of confusion is the existance of Local property on the DbSet<> object. EDIT1另一个混淆点是DbSet<>对象上存在Local属性。 It invites me to use a long running context, as another user has posted here . 它邀请我使用长时间运行的上下文,正如另一位用户在此处发布的那样。

Problem with long running context is that it doesn't refresh data - I more discussed problems here . 长时间运行的上下文的问题是它不刷新数据 - 我在这里讨论了更多的问题。 So if your user opens the list and modify data half an hour she doesn't know about changes. 因此,如果您的用户打开列表并修改数据半小时,她就不知道更改。 But in case of WPF if your business action is: 但是如果您的业务行为是WPF,则为WPF:

  • Open the list 打开列表
  • Do as many actions as you want 尽可能多地执行操作
  • Trigger saving changes 触发保存更改

Then this whole is unit of work and you can use single context instance for that. 然后这整个是工作单元,你可以使用单个上下文实例。 If you have scenario where last edit wins you should not have problems with this until somebody else deletes record which current user edits. 如果您有上次编辑获胜的场景,则在其他人删除当前用户编辑的记录之前,不应该遇到此问题。 Additionally after saving or cancelling changes you should dispose current context and load data again - this will ensure that you really have fresh data for next unit of work. 此外,在保存或取消更改后,您应该再次处理当前上下文和加载数据 - 这将确保您确实拥有下一个工作单元的新数据。

Context offers some features to refresh data but it only refreshes data previously loaded (without relations) so for example new unsaved records will be still included. Context提供了一些刷新数据的功能,但它只刷新以前加载的数据(没有关系),因此例如仍然会包含新的未保存记录。

Perhaps you can also read about MS Sync framework and local data cache. 也许您还可以阅读有关MS Sync框架和本地数据缓存的信息。

Sounds to me like your users could have a copy (cached) of the data for an indefinate period of time. 听起来像你的用户可能会在不确定的时间段内复制(缓存)数据。 The longer the users are using cached data the greater the odds that they could become disconnected from the database connection in DbContext. 用户使用缓存数据的时间越长,他们在DbContext中与数据库连接断开连接的可能性就越大。 My guess is EF doesn't handle this well and you probably want to deal with that. 我的猜测是EF不能很好地处理这个问题,你可能想要解决这个问题。 (eg occaisionally connected architecture). (例如, occaisionally连接架构)。 I would expect implementing that may solve many of your issues. 我希望实施可以解决你的许多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM