EntityFramework 插件之EntityFramework.Extended (批量处理)
接手了一個用EF來做的項目,由于項目中使用的原生處理,導(dǎo)致很多update都是采用先select 后 update的方式來實現(xiàn),同時無法批量執(zhí)行邏輯如:根據(jù)訂單類型統(tǒng)一更新狀態(tài)等。所以在經(jīng)過了N多查找之后 發(fā)現(xiàn)了一個國外寫的擴展插件EntityFramework.Extended 。
Github:https://github.com/loresoft/EntityFramework.Extended
簡單說一下用法:
Deleting
//delete all users where FirstName matches
context.Users
.Where(u => u.FirstName == "firstname")
.Delete();
Update
//update all tasks with status of 1 to status of 2
context.Tasks
.Where(t => t.StatusId == 1)
.Update(t => new Task { StatusId = 2 });
?
//example of using an IQueryable as the filter for the update
var users = context.Users.Where(u => u.FirstName == "firstname");
context.Users.Update(users, u => new User {FirstName = "newfirstname"});
看示例代碼就已經(jīng)很直觀了。
還有2個很少被人提到的功能:
將來的查詢
建立對你所需要的數(shù)據(jù),并在第一時間在任何結(jié)果的查詢列表被訪問,所有數(shù)據(jù)都將在一個往返到數(shù)據(jù)庫服務(wù)器中檢索。降低數(shù)據(jù)查詢成本。使用此功能是附加一樣簡單.Future()您的查詢的末尾。要使用將來的查詢,確保導(dǎo)入EntityFramework.Extensions命名空間。
將來的查詢與下面的擴展方法創(chuàng)建...
- Future()
- FutureFirstOrDefault()
- FutureCount()
Demo
// build up queries var q1 = db.Users .Where(t => t.EmailAddress == "one@test.com") .Future();?
var q2 = db.Tasks .Where(t => t.Summary == "Test") .Future();?
// this triggers the loading of all the future queries var users = q1.ToList();在上面的例子中,有2個查詢建立起來的,只要查詢中的一個被枚舉,它觸發(fā)兩個查詢的批次負載。
// base query var q = db.Tasks.Where(t => t.Priority == 2); // get total count var q1 = q.FutureCount(); // get page var q2 = q.Skip(pageIndex).Take(pageSize).Future();?
// triggers execute as a batch int total = q1.Value; var tasks = q2.ToList();在這個例子中,我們必須的任務(wù)列表共同senerio。為了使GUI設(shè)置尋呼控制,你需要一個總數(shù)。隨著未來我們可以批量在一起的查詢來獲得一個數(shù)據(jù)庫調(diào)用的所有數(shù)據(jù)。
將來的查詢通過創(chuàng)建保持IQuerable適當IFutureQuery對象工作。然后IFutureQuery對象存儲在IFutureContext.FutureQueries列表。然后,當IFutureQuery對象之一被枚舉,它調(diào)用回IFutureContext.ExecuteFutureQueries()經(jīng)由LoadAction委托。ExecuteFutureQueries建立從所有的存儲IFutureQuery對象批量查詢。最后,所有的IFutureQuery對象與從查詢的結(jié)果進行更新。
查詢結(jié)果緩存
緩存查詢結(jié)果,請使用FromCache位于擴展方法EntityFramework.Extensions命名空間。下面是一個示例高速緩存查詢結(jié)果。簡單地構(gòu)建LINQ查詢,你通常會,然后追加的FromCache擴展。
//query is cached using the default settings var tasks = db.Tasks .Where(t => t.CompleteDate == null) .FromCache();?
//query result is now cached 300 seconds var tasks = db.Tasks .Where(t => t.AssignedId == myUserId && t.CompleteDate == null) .FromCache(CachePolicy.WithDurationExpiration(TimeSpan.FromSeconds(300)));查詢結(jié)果Cache也支持標記緩存,以便您可以通過調(diào)用過期常見的緩存條目Expire上的高速緩存標記。
// cache assigned tasks var tasks = db.Tasks .Where(t => t.AssignedId == myUserId && t.CompleteDate == null) .FromCache(tags: new[] { "Task", "Assigned-Task-" + myUserId });?
// some update happened to Task, expire Task tag CacheManager.Current.Expire("Task");在CacheManager對供應(yīng)商的支持。默認提供程序使用MemoryCache存儲緩存條目。要創(chuàng)建一個自定義的供應(yīng)商,實施ICacheProvider。然后,自定義提供程序?qū)⑿枰诘怯?/span>Locator容器。
// Replace cache provider with Memcached provider Locator.Current.Register<ICacheProvider>(() => new MemcachedProvider());審計日志
審計日志功能將捕捉到的變化隨時它們被提交到數(shù)據(jù)庫實體。審核日志僅捕獲那些上發(fā)生了變化,這些變化的實體,只有屬性的實體。該前和記錄值之后,?AuditLogger.LastAudit就是在這個信息被舉行,是一個ToXml()可以很容易把審計日志轉(zhuǎn)換為XML,便于儲存方法。
審計日志可以通過在實體上或通過流利的配置API的屬性自定義。
流利的配置
// config audit when your application is starting up... var auditConfiguration = AuditConfiguration.Default;?
auditConfiguration.IncludeRelationships = true; auditConfiguration.LoadRelationships = true; auditConfiguration.DefaultAuditable = true;?
// customize the audit for Task entity auditConfiguration.IsAuditable<Task>() .NotAudited(t => t.TaskExtended) .FormatWith(t => t.Status, v => FormatStatus(v));?
// set the display member when status is a foreign key auditConfiguration.IsAuditable<Status>() .DisplayMember(t => t.Name);創(chuàng)建審核日志
var db = new TrackerContext(); var audit = db.BeginAudit();?
// make some updates ...?
db.SaveChanges(); var log = audit.LastLog;?
問題歸納
?
轉(zhuǎn)載于:https://www.cnblogs.com/sephiroth-wzc/p/5798996.html
總結(jié)
以上是生活随笔為你收集整理的EntityFramework 插件之EntityFramework.Extended (批量处理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么叫“默认邮件客户端没有正确安装”?
- 下一篇: selenium自动化_如何启动safa