• Stars
    star
    1,339
  • Rank 35,100 (Top 0.7 %)
  • Language
    C#
  • License
    BSD 3-Clause "New...
  • Created about 13 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add-on feature for Entity Framework

Library Powered By

This library is powered by Entity Framework Extensions

Entity Framework Extensions

IMPORTANT: This library is no longer supported since 2015.

We highly recommend you to move to:

Entity Framework Extensions

Website: https://entityframework-extensions.net/

Paid library to dramatically improve Entity Framework performance:

  • BulkSaveChanges
  • BulkInsert
  • BulkUpdate
  • BulkDelete
  • BulkMerge
  • BulkSynchronize

Entity Framework Plus

Website: https://entityframework-plus.net/

Free & Open source library that support following features:

  • Audit
  • Batch Operations
    • Batch Delete
    • Batch Update
  • Query
    • Query Cache
    • Query Deferred
    • Query Filter
    • Query Future
    • Query IncludeFilter
    • Query IncludeOptimized

What's Entity Framework Extended?

Download

The Entity Framework Extended library is available on nuget.org via package name EntityFramework.Extended.

To install EntityFramework.Extended, run the following command in the Package Manager Console.

PM> Install-Package EntityFramework.Extended

Features

Batch Update and Delete

The Entity Framework's current limitation is that you have first to retrieve it into memory to update or delete an entity. Now in most scenarios, this is just fine. There are, however, some scenarios where performance would suffer. Also, the object must be retrieved for single deletes before it can be deleted, requiring two calls to the database. Batch update and delete eliminate the need to retrieve and load an entity before modifying it.

Deleting

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

Update

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

Future Queries

Build up a list of queries for the data that you need, and the first time any of the results are accessed, all the data will retrieved in one round trip to the database server. Reducing the number of trips to the database is a great. Using this feature is as simple as appending .Future() to the end of your queries to use the Future Queries.

Future queries are created with the following extension methods:

  • Future()
  • FutureFirstOrDefault()
  • FutureCount()

Sample

// build up queries
var q1 = db.Users
    .Where(t => t.EmailAddress == "[email protected]")
    .Future();

var q2 = db.Tasks
    .Where(t => t.Summary == "Test")
    .Future();

// this triggers the loading of all the future queries
var users = q1.ToList();

In the example above, there are two queries built up. As soon as one of the queries is enumerated, it triggers the batch load of both queries.

// base query
var q = db.Tasks.Where(t => t.Priority == 2);
// get total count
var q1 = q.FutureCount();
// get page
var q2 = q.Skip(pageIndex).Take(pageSize).Future();

// triggers execute as a batch
int total = q1.Value;
var tasks = q2.ToList();

In this example, we have a common scenario where you want to page a list of tasks. For the GUI to set up the paging control, you need a total count. With Future, we can batch together the queries to get all the data in one database call.

Future queries work by creating the appropriate IFutureQuery object that keeps the IQuerable. The IFutureQuery object is then stored in IFutureContext.FutureQueries list. Then, when one of the IFutureQuery objects is enumerated, it calls back to IFutureContext.ExecuteFutureQueries() via the LoadAction delegate. ExecuteFutureQueries builds a batch query from all the stored IFutureQuery objects. Finally, all the IFutureQuery objects are updated with the results from the query.

Query Result Cache

To cache query results, use the FromCache extension method. Below is a caching query result. Construct the LINQ query as you normally would, then append the FromCache extension.

//query is cached using the default settings
var tasks = db.Tasks
    .Where(t => t.CompleteDate == null)
    .FromCache();

//query result is now cached 300 seconds
var tasks = db.Tasks
    .Where(t => t.AssignedId == myUserId && t.CompleteDate == null)
    .FromCache(CachePolicy.WithDurationExpiration(TimeSpan.FromSeconds(300)));

The Query Result Cache also supports tagging the cache so you can expire common cache entries by calling Expire on a cache tag.

// 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");

The CacheManager has support for providers. The default provider uses MemoryCache to store the cache entries. To create a custom provider, implement ICacheProvider. The custom provider will then need to be registered in the Locator container.

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

Audit Log

The Audit Log feature will capture the changes to entities anytime they are submitted to the database. The Audit Log captures only the changed entities and only the changed properties. The before and after values are recorded. AuditLogger.LastAudit is where this information is held and there is a ToXml() method that makes it easy to turn the AuditLog into xml for easy storage.

The AuditLog can be customized via attributes on the entities or via a Fluent Configuration API.

Fluent Configuration

// 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);

Create an Audit Log

var db = new TrackerContext();
var audit = db.BeginAudit();

// make some updates ...

db.SaveChanges();
var log = audit.LastLog;

Useful links

Contribute

Want to help us? Your donation directly helps us maintain and grow ZZZ Free Projects.

We can't thank you enough for your support 🙏.

👍 One-time donation

❤️ Become a sponsor

Why should I contribute to this free & open-source library?

We all love free and open-source libraries! But there is a catch... nothing is free in this world.

We NEED your help. Last year alone, we spent over 3000 hours maintaining all our open source libraries.

Contributions allow us to spend more of our time on: Bug Fix, Development, Documentation, and Support.

How much should I contribute?

Any amount is much appreciated. All our free libraries together have more than 100 million downloads.

If everyone could contribute a tiny amount, it would help us make the .NET community a better place to code!

Another great free way to contribute is spreading the word about the library.

A HUGE THANKS for your help!

More Projects

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

More Repositories

1

html-agility-pack

Html Agility Pack (HAP) is a free and open-source HTML parser written in C# to read/write DOM and supports plain XPATH or XSLT. It is a .NET code library that allows you to parse "out of the web" HTML files.
C#
2,635
star
2

EntityFramework-Plus

Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more
C#
2,251
star
3

Z.ExtensionMethods

C# Extension Methods | Over 1000 extension methods:
C#
1,574
star
4

System.Linq.Dynamic.Core

The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
C#
1,555
star
5

EntityFramework.DynamicFilters

Global filtering for Entity Framework.
C#
501
star
6

Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
C#
452
star
7

EntityFramework-Effort

Entity Framework Effort is a powerful tool that enables a convenient way to create automated tests for Entity Framework based applications.
C#
431
star
8

System.Linq.Dynamic

[Deprecated] This is the Microsoft assembly for the .Net 4.0 Dynamic language functionality.
425
star
9

Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
C#
384
star
10

EntityFramework-Extensions

Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
C#
344
star
11

GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
C#
333
star
12

sqlfiddle3

New version based on vert.x and docker
JavaScript
331
star
13

sqlfiddle

JavaScript
275
star
14

findandreplace

fnr.exe - Find and Replace (FNR) is an open source tool to find and replace text in multiple files. It can quickly search through large numbers of files and also find the information using regular expressions specifying the form of what you want, instead of literal text.
C#
243
star
15

nmemory

NMemory is a lightweight non-persistent in-memory relational database engine that is purely written in C# and can be hosted by .NET applications.
C#
232
star
16

sqlfiddle2

New version of SQL Fiddle based on OpenIDM
JavaScript
195
star
17

Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
C#
142
star
18

zzzcode.ai

AI Website for developers. Use ChatGPT to answer questions, write code, and more.
108
star
19

EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
C#
102
star
20

LINQ-Async

C# LINQ Async extension methods library for async/await task.
C#
101
star
21

Eval-SQL.NET

SQL Eval Function | Dynamically Evaluate Expression in SQL Server using C# Syntax.
C#
95
star
22

EntityFrameworkExtras

EntityFrameworkExtras provides some useful additions to EntityFramework such as executing Stored Procedures with User-Defined Table Types and Output Parameters.
C#
80
star
23

awesome-entity-framework-core

Awesome EF Core third party-libraries, documentation, and tutorials!
57
star
24

Dapper.Transaction

IDbTransaction extension methods for Dapper: A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..
C#
29
star
25

awesome-dapper

Awesome Dapper third party-libraries, documentation, and tutorials!
16
star
26

EntityFramework6-DotNetStandard

The support has been moved here: https://github.com/zzzprojects/EntityFramework-Classic
C#
13
star
27

awesome-entity-framework-6

Awesome EF6 third party-libraries, documentation, and tutorials!
12
star
28

Compiler-Expression.NET

C# Code Compiler | Code Analysis and Code Compiler for Eval-Expression.NET and Eval-SQL.NET
12
star
29

awesome-csharp-expression-evaluator

Awesome C#/.NET Expression Evaluator third party-libraries, documentation, and tutorials!
8
star
30

LinqToSql-Plus

LinqToSql Plus is a library that dramatically improves LinqToSql performances by using bulk and batch operations.
6
star
31

docs

5
star
32

learn-orm

learn-orm
C#
5
star
33

sqlite-provider

sqlite-provider
C
2
star