Entity Framework Extended Library

扩展了实体框架的功能类库.

https://github.com/loresoft/EntityFramework.Extended

1、批量更新/删除

1)删除

//delete all users where FirstName matches
context.Users.Delete(u => u.FirstName == "firstname");

2)更新

//update all tasks with status of 1 to status of 2
context.Tasks.Update(
    t => t.StatusId == 1, 
    t2 => 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"});

2、查询结果缓存

存查询结果,使用扩展方法FromCache位于EntityFramework。命名空间扩展。下面是一个示例缓存查询结果。构建LINQ查询,然后追加的FromCache延伸。

1)默认设置

//query is cached using the default settings  //查询缓存是使用默认设置
var tasks = db.Tasks
    .Where(t => t.CompleteDate == null)
    .FromCache();

//query result is now cached 300 seconds //现在是300秒的缓存查询结果
var tasks = db.Tasks
    .Where(t => t.AssignedId == myUserId && t.CompleteDate == null)
    .FromCache(CachePolicy.WithDurationExpiration(TimeSpan.FromSeconds(300)));
//删除缓存
 db.Tasks.Where(t => t.AssignedId == myUserId && t.CompleteDate == null).RemoveCache();

2)标记缓存

查询结果缓存还支持标记缓存,这样您就可以通过调用过期的缓存标记过期公共缓存条目。

// 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支持提供程序使用MemoryCache存储高速缓存条目。实现自定义提供程序,实现ICacheProvider自定义提供程序将需要在定位器解析器注册。

// Replace cache provider with Memcached provider   替换缓存
Locator.Current.Register<ICacheProvider>(() => new MemcachedProvider());

默认缓存策略

当没有CachePolicy设置,使用默认的CachePolicy.Default你可以设置CachePolicy.Default值。在应用程序启动时默认为默认设置。

3、检查日志

能将在任何时间提交到数据库时捕捉到实体的更改。检查日志只捕获被改变的实体,只捕获那些被改变的实体的属性。记录前后的值。在这个信息AuditLogger.LastAudit是举行并有一个可以很容易的把检查日志为XML便于储存ToXml()方法。

检查日志可以通过定制的实体或属性通过Fluent 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);

创建检查日志

var db = new TrackerContext();
var audit = db.BeginAudit();
// make some updates ... 一些代码 db.SaveChanges(); var log = audit.LastLog;
原文地址:https://www.cnblogs.com/w2011/p/4766628.html