实体框架4.1 DbSet刷新 [英] Entity Framework 4.1 DbSet Reload

查看:110
本文介绍了实体框架4.1 DbSet刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的DbContext 方案的单个实例在一个WPF应用程序在本地影数据库的完整副本。我听说这是不好的做法,但我的数据库是小,我需要的是一个完整的副本本地应用程序运行时。

I'm using a single instance of DbContext scenario to shadow entire copy of the database locally in a WPF app. I've heard this is bad practice, but my database is small and I need an entire copy of it locally while the app is running.

的IQueryable 的扩展方法Load()让我preLOAD的元素 DbSet<> ,这样我可以绑定事物 DbSet&LT的本地地产;> 。数据库中的数据变化迅速,所以我想的SaveChanges()和重装的所有,即使是已经被跟踪的对象。调用加载()方法再次不更新这些已经加载了跟踪,但没有标记为已更改的项目。

An extension method for IQueryable, Load() lets me preload the elements of a DbSet<>, so that I can bind things to the Local property of DbSet<>. Data in the database changes rapidly, so I want to SaveChanges() and reload everything, even objects that are already tracked. Calling the Load() method again doesn't update the items that are tracked but are not marked as changed, which are already loaded.

什么是在 DbSet&LT重装preloaded项目的preferred方法;&GT; ?关闭我的头顶,我只能想到调用的的SaveChanges(),然后通过所有条目,并在数据库中被跟踪和原始值设置为当前值,那么加载()什么新的可能已添加的对象。在我的情况是无法删除的对象,但我可能会支持项目缺失,从长远来看。这似乎并不正确,应该放下一切,重新加载的方法。这似乎更容易放弃我的背景和刚开始新生活,但在WPF中的所有元素都已经被绑定到 Local'ObservableCollection&LT;&GT; ,这只是混乱了接口。

What is the preferred method of reloading the preloaded items in a DbSet<>? Off the top of my head, I can only think of calling SaveChanges(), then go through all entries and set both tracked and original values to the current values in the database, then Load() whatever new objects that might have been added. In my scenario it's not possible to delete objects, but I might have to support item deletion in the long run. This doesn't seem right, there should be a way to drop everything and reload. It would seem that it's easier to drop my context and just start anew, but all the elements in WPF are already bound to the Local´ObservableCollection<>, and this just messes up the interface.

推荐答案

这是不是你应该使用的DbContext ,因为和方式,这几乎是不可能的重新加载数据。保持一个单一的环境很长一段时间是<一个href=\"http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392\">incorrect使用。链接也将回答为什么不更新你跟踪的实体。

This is not the way you are supposed to use DbContext, and because of that it is almost impossible to reload the data. Keeping a single context around for a long time is incorrect usage. The link will also answer why your tracked entities are not updated.

您可以通过调用刷新 DbEntityEntry 选择性地重新加载一个单一的实体

You can selectively reload a single entity by calling Reload on DbEntityEntry:

context.Entry(entity).Reload();

您也可以恢复到的ObjectContext ,并使用的ObjectQuery MergeOption.OverrideChanges ,或使用刷新为实体与集合 RefreshMode.StoreWins

You can also revert back to ObjectContext and use ObjectQuery with MergeOption.OverrideChanges, or use Refresh for a collection of entities with RefreshMode.StoreWins.

所有这些方法都受到一些问题:

All these approaches suffers some problems:


  • 如果该记录在数据库中删除,但不会从上下文中删除。

  • 在关系的变化并不总是刷新。

获取新数据的唯一正确途径是的Dispose 的背景下,创建一个新的并加载一切从头开始 - 和你反正做这个

The only correct way to get fresh data is Dispose the context, create a new one and load everything from scratch - and you are doing this anyway.

这篇关于实体框架4.1 DbSet刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆