Skip to content

Instantly share code, notes, and snippets.

View JonathanMagnan's full-sized avatar

Jonathan Magnan JonathanMagnan

View GitHub Profile
// using Z.EntityFramework.Extensions; // Don't forget to include this.
// Easy to use
context.BulkMerge(list);
// Easy to customize
context.BulkMerge(list, bulk => bulk.BatchSize = 100);
// using Z.EntityFramework.Extensions; // Don't forget to include this.
// Easy to use
context.BulkSaveChanges();
// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);
// using Z.EntityFramework.Extensions; // Don't forget to include this.
// Easy to use
context.BulkUpdate(list);
// Easy to customize
context.BulkUpdate(list, bulk => bulk.BatchSize = 100);
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// 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();
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// The count is deferred and cached.
var count = ctx.Customers.DeferredCount().FromCache();
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
ctx.Filter<Post>(q => q.Where(x => !x.IsSoftDeleted));
// SELECT * FROM Post WHERE IsSoftDeleted = false
var list = ctx.Posts.ToList();
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// 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
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// 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();
// SELECT * FROM Order WHERE....
// SELECT * FROM OrderItem WHERE EXISTS (/* previous query */) AND ...
// SELECT * FROM DeliveryItems WHERE EXISTS (/* previous query */) AND ...
var orders = ctx.Orders
.Where(x => x.OrderId == myOrderID) // 1 orders, 20 columns
.IncludeOptimized(x => x.Items) // 20 items, 10 columns
.IncludeOptimized(x => x.DeliveredItems) // 10 items, 10 columns
.ToList();
var ctx = new EntitiesContext();
ctx.Customers.AddRange(listToAdd); // add
ctx.Customers.RemoveRange(listToRemove); // remove
listToModify.ForEach(x => x.DateModified = DateTime.Now); // modify
// Easy to use
ctx.BulkSaveChanges();
// Easy to customize