且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF4 Code First的通用存储库

更新时间:2023-02-13 15:36:40

嗨尼克

EF保留已查询的所有对象的缓存,因此如果您查询同一对象两次,它将为您提供相同的实例。您的更改尚未写入数据库,但因为第二次使用相同的主
键查询对象时,结果集中将显示已存在于内存中的实例。

EF keeps a cache of all objects that have been queried for, so if you query for the same object twice it will give you the same instance. Your changes haven't been written to the database but because the second time you query the object with the same primary key is present in the result set it will use the instance that is already in memory.

你可以让EF覆盖任何未保存的更改,如下所示:

You can get EF to overwrite any un-saved changes as follows:


var set = this.ObjectContext.CreateObjectSet<T>();
set.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
return set.ToList();