博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]Entity Framework Refresh context?
阅读量:5941 次
发布时间:2019-06-19

本文共 1237 字,大约阅读时间需要 4 分钟。

本文转自:

 

If you want to reload specific entities, with the DbContextApi, RX_DID_RX already gave you the answer.

If you want to reload / refresh all the entities you loaded:

If you are using Entity Framework 4.1+ (EF5, or EF 6 probably), DbContext API:

public void RefreshAll(){     foreach (var entity in ctx.ChangeTracker.Entries())     {           entity.Reload();     }}

Use the method:

context.Refresh(RefreshMode.StoreWins, yourEntity);

or in alternative dispose your current context and create a new one.

 

 

 

context.Reload() was not working for me in MVC 4, EF 5 so I did this.

context.Entry(entity).State = EntityState.Detached;entity = context.Find(entity.ID); http://stackoverflow.com/questions/15828811/entity-framework-caching-issue
 

If you know that changes happened outside of EF and want to refresh your ctxt for a specific entity, you can call

datamodel.Refresh(RefreshMode.StoreWins, orders);

If this seems like it will be a common occurance, you should disable object caching in your queries:

SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();datamodel.tblCities.MergeOption = MergeOption.NoTracking;

or for to turn off object level caching for specific Entity,

Context.Set
().AsNoTracking();

 

转载地址:http://lpmtx.baihongyu.com/

你可能感兴趣的文章
扩展运算符和解构赋值的理解
查看>>
手机H5显示一像素的细线
查看>>
Menu 菜单栏
查看>>
Integer跟int的区别(备份回忆)
查看>>
集合解析
查看>>
详解分布式应用程序协调服务Zookeeper
查看>>
软件工程之构建之法
查看>>
UVa 10902
查看>>
Mathf.Sin正弦
查看>>
禁止浏览器缓存js
查看>>
【Redis】安装PHP的redis驱动(二)
查看>>
java中string和int互相转化
查看>>
什么是序列化,为什么要序列化
查看>>
Java保留小数点后有效数字
查看>>
C++中一些类和数据结构的大小的总结
查看>>
mysql开启binlog
查看>>
ctrl + z fg bg
查看>>
工作流引擎Oozie(一):workflow
查看>>
struct框架
查看>>
Deep Learning(深度学习)相关网站
查看>>