EF4 Code First的通用存储库 [英] Generic Repository with EF4 Code First

查看:67
本文介绍了EF4 Code First的通用存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


public
 IEnumerable<T> FindAll<T>() where
 T : class

  {
   return
 ObjectContext.CreateObjectSet<T>().ToList();
  }

推荐答案

嗨尼克

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();


这篇关于EF4 Code First的通用存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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