# EntityFramework-Plus **Repository Path**: anysharp/EntityFramework-Plus ## Basic Information - **Project Name**: EntityFramework-Plus - **Description**: 开源、免费(MIT License)、功能强大的 Entity Framework(EF)和 Entity Framework Core(EF Core) 扩展库,旨在提升 Entity Framework 的性能和克服其局限性。通过提供一系列实用的功能,如批量操作、查询缓存、查询延迟、LINQ动态、审计跟踪等,使得使用 Entity Framework 进行数据库开发变得更加高效和灵活。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: https://www.cnblogs.com/Can-daydayup/p/18411468 - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-11 - **Last Updated**: 2024-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: ORM, EFCore ## README ### Library Powered By This library is powered by [Entity Framework Extensions](https://entityframework-extensions.net/?z=github&y=entityframework-plus) 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 - Batch Operations - [Batch Delete](https://entityframework-plus.net/ef-core-batch-delete) - [Batch Update](https://entityframework-plus.net/ef-core-batch-update) - LINQ - [LINQ Dynamic](https://entityframework-plus.net/ef-core-linq-dynamic) - Query - [Query Cache](https://entityframework-plus.net/ef-core-query-cache) - [Query Deferred](https://entityframework-plus.net/ef-core-query-deferred) - [Query DbSetFilter](https://entityframework-plus.net/query-db-set-filter) - [Query Filter](https://entityframework-plus.net/ef-core-query-filter) - [Query Future](https://entityframework-plus.net/ef-core-query-future) - [Query IncludeFilter](https://entityframework-plus.net/ef-core-query-include-filter) - [Query IncludeOptimized](https://entityframework-plus.net/ef-core-query-include-optimized) - [Audit](https://entityframework-plus.net/ef-core-audit) --- **Bulk Operations only available with [Entity Framework Extensions](http://entityframework-extensions.net/)** - BulkSaveChanges - BulkInsert - BulkUpdate - BulkDelete - BulkMerge --- ## Batch Delete Deletes multiples rows in a single database roundtrip and without loading entities in the context. ```csharp // using Z.EntityFramework.Plus; // Don't forget to include this. // DELETE all users which has been inactive for 2 years ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2)) .Delete(); // DELETE using a BatchSize ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2)) .Delete(x => x.BatchSize = 1000); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-batch-delete)** ## Batch Update Updates multiples rows using an expression in a single database roundtrip and without loading entities in the context. ```csharp // using Z.EntityFramework.Plus; // Don't forget to include this. // UPDATE all users which has been inactive for 2 years ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2)) .Update(x => new User() { IsSoftDeleted = 1 }); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-batch-update)** ## 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_ ```csharp // The query is cached using default QueryCacheManager options var countries = ctx.Countries.Where(x => x.IsActive).FromCache(); // (EF5 | EF6) The query is cached for 2 hours var states = ctx.States.Where(x => x.IsActive).FromCache(DateTime.Now.AddHours(2)); // (EF7) The query is cached for 2 hours without any activity var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2)}; var states = ctx.States.Where(x => x.IsActive).FromCache(options); ``` _Cache Tags_ ```csharp var states = db.States.Where(x => x.IsActive).FromCache("countries", "states"); var stateCount = db.States.Where(x => x.IsActive).DeferredCount().FromCache("countries", "states"); // Expire all cache entry using the "countries" tag QueryCacheManager.ExpireTag("countries"); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-cache)** ## Query Deferred **Defer the execution of a query which is normally executed to allow some features like Query Cache and Query Future.** ```csharp // Oops! The query is already executed, we cannot use Query Cache or Query Future features var count = ctx.Customers.Count(); // Query Cache ctx.Customers.DeferredCount().FromCache(); // Query Future ctx.Customers.DeferredCount().FutureValue(); ``` > All LINQ extensions are supported: Count, First, FirstOrDefault, Sum, etc. **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-deferred)** ## Query Filter **Filter query with predicate at global, instance or query level.** **Support:** _Global Filter_ ```csharp // CREATE global filter QueryFilterManager.Filter(x => x.Where(c => c.IsActive)); var ctx = new EntityContext(); // TIP: Add this line in EntitiesContext constructor instead QueryFilterManager.InitilizeGlobalFilter(ctx); // SELECT * FROM Customer WHERE IsActive = true var customer = ctx.Customers.ToList(); ``` _Instance Filter_ ```csharp var ctx = new EntityContext(); // CREATE filter ctx.Filter(x => x.Where(c => c.IsActive)); // SELECT * FROM Customer WHERE IsActive = true var customer = ctx.Customers.ToList(); ``` _Query Filter_ ```csharp var ctx = new EntityContext(); // CREATE filter disabled ctx.Filter(CustomEnum.EnumValue, x => x.Where(c => c.IsActive), false); // SELECT * FROM Customer WHERE IsActive = true var customer = ctx.Customers.Filter(CustomEnum.EnumValue).ToList(); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-filter)** ## 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_ ```csharp // GET the states & countries list var futureCountries = db.Countries.Where(x => x.IsActive).Future(); var futureStates = db.States.Where(x => x.IsActive).Future(); // TRIGGER all pending queries (futureCountries & futureStates) var countries = futureCountries.ToList(); ``` _FutureValue_ ```csharp // GET the first active customer and the number of avtive customers var futureFirstCustomer = db.Customers.Where(x => x.IsActive).DeferredFirstOrDefault().FutureValue(); var futureCustomerCount = db.Customers.Where(x => x.IsActive).DeferredCount().FutureValue(); // TRIGGER all pending queries (futureFirstCustomer & futureCustomerCount) Customer firstCustomer = futureFirstCustomer.Value; ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-future)** ## 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: ```csharp var ctx = new EntityContext(); // Load only active comments var posts = ctx.Post.IncludeFilter(x => x.Comments.Where(x => x.IsActive)); ``` **Support:** EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-include-filter)** ## Query IncludeOptimized Improve SQL generate by Include and filter child collections at the same times! ```csharp var ctx = new EntityContext(); // Load only active comments using an optimized include var posts = ctx.Post.IncludeOptimized(x => x.Comments.Where(x => x.IsActive)); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-query-include-optimized)** ## 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 ```csharp // using Z.EntityFramework.Plus; // Don't forget to include this. var ctx = new EntityContext(); // ... ctx changes ... 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) { } } ``` AutoSave audit in your database ```csharp AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) => (context as EntityContext).AuditEntries.AddRange(audit.Entries); ``` **Support:** EF5, EF6, EF Core **[Learn more](https://entityframework-plus.net/ef-core-audit)** ## Useful links - [Website](https://entityframework-plus.net/) - [Documentation](https://entityframework-plus.net/batch-delete) - [Online Examples](https://entityframework-plus.net/online-examples) - You can also consult tons of Entity Framework Plus questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/entity-framework-plus) ## 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 - Projects: - [EntityFramework Extensions](https://entityframework-extensions.net/) - [Dapper Plus](https://dapper-plus.net/) - [C# Eval Expression](https://eval-expression.net/) - Learn Websites - [Learn EF Core](https://www.learnentityframeworkcore.com/) - [Learn Dapper](https://www.learndapper.com/) - Online Tools: - [.NET Fiddle](https://dotnetfiddle.net/) - [SQL Fiddle](https://sqlfiddle.com/) - [ZZZ Code AI](https://zzzcode.ai/) - and much more! To view all our free and paid projects, visit our website [ZZZ Projects](https://zzzprojects.com/).