Entity Framework Plus

Download Count:
EASY TO
USE
Extend Entity Framework DbContext
// DELETE all users which are inactive for 2 years
ctx.Users.Where(x =>
x.LastLoginDate < DateTime.Now.AddYears(-2))
.Delete();

// UPDATE all users which are inactive for 2 years
ctx.Users.Where(x =>
x.LastLoginDate < DateTime.Now.AddYears(-2))
.Update(x => new User() { IsSoftDeleted = 1 });
EASY TO
CUSTOMIZE
Flexible and feature-rich API
// DELETE using a BatchSize
ctx.Users.Where(x =>
x.LastLoginDate < DateTime.Now.AddYears(-2))
.Delete(x => x.BatchSize = 1000);

//LINQ Dynamic
var list = ctx.SelectDynamic(x =>
"new { y = x + 1 }").ToList();
var list = ctx.SelectDynamic(x =>
"new { y = x + 1 }",
new { y = 1 }).ToList();

Auditing

Improve security and know what, when and who did a changes in the context.

  • AutoSave audit values in the database
  • Keep track of SoftDelete entities
  • Filter events you desire
  • Include entity type you desire
  • Format value in your specific format
Auditing Example
var audit = new Audit();
audit.CreatedBy = "ZZZ Projects"; // Optional
ctx.SaveChanges(audit);

// Access to all auditing information
var entries = audit.Entries;
foreach(var entry in entries)
{
foreach(var property in entry.Properties)
{
}
}

Delete without loading entities

Delete rows from LINQ Query in a single database round trip without loading entities in the context.

  • Use Async methods to make your application responsive
  • Use batch size to improve performance
  • Use batch delay interval to reduce server load
  • Use Intercept to customize DbCommand
Delete Example
/ DELETE all users inactive for 2 years
var date = DateTime.Now.AddYears(-2);
ctx.Users.Where(x => x.LastLoginDate < date)
.Delete();

// DELETE using a BatchSize
var date = DateTime.Now.AddYears(-2);
ctx.Users.Where(x => x.LastLoginDate < date)
.Delete(x => x.BatchSize = 1000);

Update without loading entities

Update rows from LINQ Query in a single database round trip without loading entities in the context.

  • Use Async methods to make your application responsive
  • Use Intercept to customize DbCommand
Update Example
// UPDATE all users inactive for 2 years
var date = DateTime.Now.AddYears(-2);
ctx.Users.Where(x => x.LastLoginDate < date)
.Update(x => new User() { IsSoftDeleted = 1 });

Second Level Cache

Improve application performance and reduce sql server load by using a second level caching.

  • Use Cache Tag to expire cache
  • Use Cache Policy to control caching
Cache Example
// The first call perform a database round trip
var countries1 = ctx.Countries.FromCache().ToList();

// Subsequent calls will take the value from the memory instead
var countries2 = ctx.Countries.FromCache().ToList();

Filtering

Improve your context extensibility and create filters to query only what's really available.

  • Create Multi-Tenancy application
  • Exclude soft deleted record
  • Filter record by security access
Filtering Example
QueryFilterManager.Filter<Post>(q => q.Where(x => !x.IsSoftDeleted));

var ctx = new EntitiesContext();

// SELECT * FROM Post WHERE IsSoftDeleted = false
var list = ctx.Posts.ToList();

Future & FutureValue

Delay queries execution and batch all queries in a single database round trip.

  • Query Future
  • Query Future Value
  • Query Future Value Deferred
  • Include entity type you desire
  • Format value in your specific format
FutureValue Example
// CREATE a pending list of future queries
var futureCountries = db.Countries.Where(x => x.IsActive).Future();
var futureStates = db.States.Where(x => x.IsActive).Future();

// TRIGGER all pending queries in one database round trip
// SELECT * FROM Country WHERE IsActive = true;
// SELECT * FROM State WHERE IsActive = true
var countries = futureCountries.ToList();

// futureStates is already resolved and contains the result
var states = futureStates.ToList();

Filter included related entities

Overcome Include method limitations by filtering included related entities.

  • Filter child entities with IncludeFilter
  • Improve performance with IncludeOptimized
Filter Example
// LOAD orders and the first 10 active related entities.
var list = ctx.Orders.IncludeFilter(x => x.Items
.Where(y => !y.IsSoftDeleted)
.OrderBy(y => y.Date)
.Take(10))
.ToList();