项目作者: zzzprojects

项目描述 :
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more
高级语言: C#
项目地址: git://github.com/zzzprojects/EntityFramework-Plus.git
创建时间: 2015-12-02T21:12:52Z
项目社区:https://github.com/zzzprojects/EntityFramework-Plus

开源协议:MIT License

下载


Library Powered By

This library is powered by Entity Framework Extensions



Entity Framework Extensions


Entity Framework Plus

Improve Entity Framework performance and overcome limitations with MUST-HAVE features

Download

Full Version NuGet NuGet Install
Z.EntityFramework.Plus.EFCore download PM> Install-Package Z.EntityFramework.Plus.EFCore
Z.EntityFramework.Plus.EF6 download PM> Install-Package Z.EntityFramework.Plus.EF6
Z.EntityFramework.Plus.EF5 download PM> Install-Package Z.EntityFramework.Plus.EF5

More download options (Full and Standalone Version)

Features


Bulk Operations only available with Entity Framework Extensions

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge

Batch Delete

Deletes multiples rows in a single database roundtrip and without loading entities in the context.

  1. // using Z.EntityFramework.Plus; // Don't forget to include this.
  2. // DELETE all users which has been inactive for 2 years
  3. ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2))
  4. .Delete();
  5. // DELETE using a BatchSize
  6. ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2))
  7. .Delete(x => x.BatchSize = 1000);

Support: EF5, EF6, EF Core

Learn more

Batch Update

Updates multiples rows using an expression in a single database roundtrip and without loading entities in the context.

  1. // using Z.EntityFramework.Plus; // Don't forget to include this.
  2. // UPDATE all users which has been inactive for 2 years
  3. ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2))
  4. .Update(x => new User() { IsSoftDeleted = 1 });

Support: EF5, EF6, EF Core

Learn more

Query Cache

Query cache is the second level cache for Entity Framework.

The result of the query is returned from the cache. If the query is not cached yet, the query is materialized and cached before being returned.

You can specify cache policy and cache tag to control CacheItem expiration.

Support:

Cache Policy

  1. // The query is cached using default QueryCacheManager options
  2. var countries = ctx.Countries.Where(x => x.IsActive).FromCache();
  3. // (EF5 | EF6) The query is cached for 2 hours
  4. var states = ctx.States.Where(x => x.IsActive).FromCache(DateTime.Now.AddHours(2));
  5. // (EF7) The query is cached for 2 hours without any activity
  6. var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2)};
  7. var states = ctx.States.Where(x => x.IsActive).FromCache(options);

Cache Tags

  1. var states = db.States.Where(x => x.IsActive).FromCache("countries", "states");
  2. var stateCount = db.States.Where(x => x.IsActive).DeferredCount().FromCache("countries", "states");
  3. // Expire all cache entry using the "countries" tag
  4. QueryCacheManager.ExpireTag("countries");

Support: EF5, EF6, EF Core

Learn more

Query Deferred

Defer the execution of a query which is normally executed to allow some features like Query Cache and Query Future.

  1. // Oops! The query is already executed, we cannot use Query Cache or Query Future features
  2. var count = ctx.Customers.Count();
  3. // Query Cache
  4. ctx.Customers.DeferredCount().FromCache();
  5. // Query Future
  6. ctx.Customers.DeferredCount().FutureValue();

All LINQ extensions are supported: Count, First, FirstOrDefault, Sum, etc.

Support: EF5, EF6, EF Core

Learn more

Query Filter

Filter query with predicate at global, instance or query level.

Support:

Global Filter

  1. // CREATE global filter
  2. QueryFilterManager.Filter<Customer>(x => x.Where(c => c.IsActive));
  3. var ctx = new EntityContext();
  4. // TIP: Add this line in EntitiesContext constructor instead
  5. QueryFilterManager.InitilizeGlobalFilter(ctx);
  6. // SELECT * FROM Customer WHERE IsActive = true
  7. var customer = ctx.Customers.ToList();

Instance Filter

  1. var ctx = new EntityContext();
  2. // CREATE filter
  3. ctx.Filter<Customer>(x => x.Where(c => c.IsActive));
  4. // SELECT * FROM Customer WHERE IsActive = true
  5. var customer = ctx.Customers.ToList();

Query Filter

  1. var ctx = new EntityContext();
  2. // CREATE filter disabled
  3. ctx.Filter<Customer>(CustomEnum.EnumValue, x => x.Where(c => c.IsActive), false);
  4. // SELECT * FROM Customer WHERE IsActive = true
  5. var customer = ctx.Customers.Filter(CustomEnum.EnumValue).ToList();

Support: EF5, EF6, EF Core

Learn more

Query Future

Query Future allow to reduce database roundtrip by batching multiple queries in the same sql command.

All future query are stored in a pending list. When the first future query require a database roundtrip, all query are resolved in the same sql command instead of making a database roundtrip for every sql command.

Support:

Future

  1. // GET the states & countries list
  2. var futureCountries = db.Countries.Where(x => x.IsActive).Future();
  3. var futureStates = db.States.Where(x => x.IsActive).Future();
  4. // TRIGGER all pending queries (futureCountries & futureStates)
  5. var countries = futureCountries.ToList();

FutureValue

  1. // GET the first active customer and the number of avtive customers
  2. var futureFirstCustomer = db.Customers.Where(x => x.IsActive).DeferredFirstOrDefault().FutureValue();
  3. var futureCustomerCount = db.Customers.Where(x => x.IsActive).DeferredCount().FutureValue();
  4. // TRIGGER all pending queries (futureFirstCustomer & futureCustomerCount)
  5. Customer firstCustomer = futureFirstCustomer.Value;

Support: EF5, EF6, EF Core

Learn more

Query IncludeFilter

Entity Framework already support eager loading however the major drawback is you cannot control what will be included. There is no way to load only active item or load only the first 10 comments.

EF+ Query Include make it easy:

  1. var ctx = new EntityContext();
  2. // Load only active comments
  3. var posts = ctx.Post.IncludeFilter(x => x.Comments.Where(x => x.IsActive));

Support: EF6, EF Core

Learn more

Query IncludeOptimized

Improve SQL generate by Include and filter child collections at the same times!

  1. var ctx = new EntityContext();
  2. // Load only active comments using an optimized include
  3. var posts = ctx.Post.IncludeOptimized(x => x.Comments.Where(x => x.IsActive));

Support: EF5, EF6, EF Core

Learn more

Audit

Allow to easily track changes, exclude/include entity or property and auto save audit entries in the database.

Support:

  • AutoSave Audit
  • Exclude & Include Entity
  • Exclude & Include Property
  • Format Value
  • Ignore Events
  • Property Unchanged
  • Soft Add & Soft Delete
  1. // using Z.EntityFramework.Plus; // Don't forget to include this.
  2. var ctx = new EntityContext();
  3. // ... ctx changes ...
  4. var audit = new Audit();
  5. audit.CreatedBy = "ZZZ Projects"; // Optional
  6. ctx.SaveChanges(audit);
  7. // Access to all auditing information
  8. var entries = audit.Entries;
  9. foreach(var entry in entries)
  10. {
  11. foreach(var property in entry.Properties)
  12. {
  13. }
  14. }

AutoSave audit in your database

  1. AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
  2. (context as EntityContext).AuditEntries.AddRange(audit.Entries);

Support: EF5, EF6, EF Core

Learn more

Contribute

The best way to contribute is by spreading the word about the library:

  • Blog it
  • Comment it
  • Star it
  • Share it

A HUGE THANKS for your help.

More Projects

To view all our free and paid projects, visit our website ZZZ Projects.