var context = ((IObjectContextAdapter)myDbContext).ObjectContext;var refreshableObjects = (from entry in context.ObjectStateManager.GetobjectStateEntrIEs( EntityState.Added | EntityState.Deleted | EntityState.ModifIEd | EntityState.Unchanged) where entry.EntityKey != null select entry.Entity);context.Refresh(RefreshMode.StoreWins,refreshableObjects);//.......................................................................foreach (var entry in this.Orm.ChangeTracker.EntrIEs()){ entry.State = EntityState.Unchanged;}this.Orm.ChangeTracker.DetectChanges();
并且唯一一个刷新我的DbContext:
foreach (var i in this.Orm.ChangeTracker.EntrIEs()) i.Reload();
但它太慢了.你能帮我选择正确的方法吗?
解决方法 我刚刚发现应该评估Enumerable结果,因为Refresh方法将其作为对象获取并且不对其进行评估.var context = ((IObjectContextAdapter)myDbContext).ObjectContext;var refreshableObjects = (from entry in context.ObjectStateManager.GetobjectStateEntrIEs( EntityState.Added | EntityState.Deleted | EntityState.ModifIEd | EntityState.Unchanged) where entry.EntityKey != null select entry.Entity).ToList();context.Refresh(RefreshMode.StoreWins,refreshableObjects);
我更喜欢以下内容:
var refreshableObjects = myDbContext.ChangeTracker.EntrIEs().Select(c=>c.Entity).ToList();context.Refresh(RefreshMode.StoreWins,refreshableObjects);总结
以上是内存溢出为你收集整理的c# – 如何刷新DbContext全部内容,希望文章能够帮你解决c# – 如何刷新DbContext所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)