简体   繁体   English

实体框架4.1 DbSet重新加载

[英]Entity Framework 4.1 DbSet Reload

I'm using a single instance of DbContext scenario to shadow entire copy of the database locally in a WPF app. 我正在使用单个DbContext场景实例在WPF应用程序中本地DbContext数据库的整个副本。 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. 我听说这是不好的做法,但我的数据库很小,我需要在应用程序运行时在本地完整复制它。

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<> . IQueryable的扩展方法, Load()允许我预加载DbSet<>的元素,这样我就可以将东西绑定到DbSet<>的Local属性。 Data in the database changes rapidly, so I want to SaveChanges() and reload everything , even objects that are already tracked. 数据库中的数据变化很快,因此我想要SaveChanges()并重新加载所有内容 ,甚至是已经跟踪过的对象。 Calling the Load() method again doesn't update the items that are tracked but are not marked as changed, which are already loaded. 再次调用Load()方法不会更新已加载但未标记为已更改的项目。

What is the preferred method of reloading the preloaded items in a DbSet<> ? 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. 在我的脑海中,我只能想到调用SaveChanges() ,然后遍历所有条目并将跟踪和原始值都设置为数据库中的当前值,然后Load()任何可能已添加的新对象。 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. 似乎更容易删除我的上下文并重新开始,但WPF中的所有元素已经绑定到Local´ObservableCollection<> ,这只是弄乱了界面。

This is not the way you are supposed to use DbContext , and because of that it is almost impossible to reload the data. 这不是您应该使用DbContext的方式,因此几乎不可能重新加载数据。 Keeping a single context around for a long time is incorrect usage . 长时间保持单个上下文是不正确的用法 The link will also answer why your tracked entities are not updated. 该链接还将回答未跟踪实体未更新的原因。

You can selectively reload a single entity by calling Reload on DbEntityEntry : 您可以通过在DbEntityEntry上调用Reload来有选择地重新加载单个实体:

context.Entry(entity).Reload();

You can also revert back to ObjectContext and use ObjectQuery with MergeOption.OverrideChanges , or use Refresh for a collection of entities with RefreshMode.StoreWins . 您还可以恢复为ObjectContext并将ObjectQueryMergeOption.OverrideChanges使用,或者将Refresh用于具有RefreshMode.StoreWins的实体RefreshMode.StoreWins

All these approaches suffers some problems: 所有这些方法都遇到了一些问题:

  • If the record is deleted in the database, it will not be removed from the context. 如果在数据库中删除该记录,则不会从上下文中删除该记录。
  • Changes in relations are not always refreshed. 关系的变化并不总是得到更新。

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. 获取新数据的唯一正确方法是Dispose上下文,创建一个新的并从头开始加载所有内容 - 无论如何,您正在执行此操作。

With Entity Framework 4.1, the recommendation for WPF data binding has changed to use .Local and a persistent DbContext. 使用Entity Framework 4.1,对WPF数据绑定的建议已更改为使用.Local和持久性DbContext。

http://blogs.msdn.com/b/efdesign/archive/2010/09/08/data-binding-with-dbcontext.aspx http://blogs.msdn.com/b/efdesign/archive/2010/09/08/data-binding-with-dbcontext.aspx

It's, of course, possible to dispose of it whenever you need to, but it can negatively impact the UI if you do. 当然,可以随时处理它,但如果你这样做,它会对UI产生负面影响。

Here's another method, but I'm not sure that it takes EF4.1's features into account: 这是另一种方法,但我不确定是否需要考虑EF4.1的功能:

http://msdn.microsoft.com/en-us/library/cc716735.aspx http://msdn.microsoft.com/en-us/library/cc716735.aspx

DbContexts are supposed to live short time, Consider after saving changes disposing it and reloading all from the start. DbContexts应该在短时间内存活,在保存更改后再考虑处理它并从头开始重新加载。 Have 2 sets of objects.. one from db and another for binding. 有2组对象..一组来自db,另一组来自绑定。

Please use using() for CRUD.It will automatically reload the updated data. 请对CRUD使用using()。它会自动重新加载更新的数据。

using (myDbContext context = new myDbContext())
{

}

Best Regards, Thet Tin Oo 最诚挚的问候,Thet Tin Oo

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

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