当然你要选择你对应的版本,并且同时你也安装了对应版本的EF版本(我选择EF6的扩展,那么我应该也对应现有的EF6)
2.操作(批量删除)
安装nuget包之后我们会发现我们平时惯用的linq表达式多了一些智能提示
好吧确实很简单,删除颜色是blue条件的所有数据
using (DBContainer ctx = new DBContainer()) { ctx.Spl_Product.Where(a => a.Color == "blue").Delete(); }
3.操作(批量更新)
批量更新创建日期3天以前的数据,让color=red,code=xxxx
using (DBContainer ctx = new DBContainer()) { ctx.Spl_Product.Where(a => a.CreateTime < DateTime.Now.AddDays(-3)).Update(a=> new Spl_Product { Color="red" , Code="xxxx"}); }
4.设置缓存
在从缓存查询之前,我们必须引用 System.Runtime.Caching来支持系统缓存(而且可以设置缓存的时间)
下面来看怎么设置缓存:
using (DBContainer ctx = new DBContainer()) { //查询数据之后并进行缓存 var list = ctx.Spl_Product.Where(x => x.Code=="red").FromCache(); // (EF5 | EF6) 让查询缓存维持2个小时 var list2 = ctx.Spl_Product.Where(x => x.Code=="red").FromCache(DateTime.Now.AddHours(2)); }
我们在查询的最后加上.FromCache和.FromCache(DateTime.Now.AddHours(2))来分别设置缓存,所以我们在第一次查询之后就可以设置缓存
using (DBContainer ctx = new DBContainer()) { // EF Core 的写法 var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2) }; var states = ctx.Spl_Product.Where(x => x.Color=="red").FromCache(options); }
5.从缓存查询
using (DBContainer ctx = new DBContainer()) { //从缓存中查询,如果没有缓存即从数据库查询 var list3 = ctx.Spl_Product.Where(x => x.Code==