Skip to content

miasvanklei/N.EntityFramework.Extensions

 
 

Repository files navigation

N.EntityFramework.Extensions


latest version

**If you are using Entity Framework Core v5.0.1+, you can use https://github.com/NorthernLight1/N.EntityFrameworkCore.Extensions

Bulk data support for the EntityFramework 6.2.0+

The framework currently supports the following operations:

Entity Framework Extensions extends your DbContext with high-performance bulk operations: BulkDelete, BulkInsert, BulkMerge, BulkSync, BulkUpdate, Fetch, FromSqlQuery, DeleteFromQuery, InsertFromQuery, UpdateFromQuery, QueryToCsvFile, SqlQueryToCsvFile

Inheritance models supported: Table-Per-Hierarchy, Table-Per-Concrete

Installation

The latest stable version is available on NuGet.

Install-Package N.EntityFramework.Extensions

Usage

BulkInsert() - Performs a insert operation with a large number of entities

var dbcontext = new MyDbContext();  
var orders = new List<Order>();  
for(int i=0; i<10000; i++)  
{  
   orders.Add(new Order { OrderDate = DateTime.UtcNow, TotalPrice = 2.99 });  
}  
dbcontext.BulkInsert(orders);  

BulkDelete() - Performs a delete operation with a large number of entities

var dbcontext = new MyDbContext();  
var orders = dbcontext.Orders.Where(o => o.TotalPrice < 5.35M);  
dbcontext.BulkDelete(orders);

BulkUpdate() - Performs a update operation with a large number of entities

var dbcontext = new MyDbContext();  
var products = dbcontext.Products.Where(o => o.Price < 5.35M);
foreach(var product in products)
{
    order.Price = 6M;
}
dbcontext.BulkUpdate(products);

BulkMerge() - Performs a merge operation with a large number of entities

var dbcontext = new MyDbContext();
var products = new List<Product>();
var existingProducts = dbcontext.Products.Where(o => o.Price < 5.35M);
foreach(var product in existingProducts)
{
    product.Price = 6M;
}
products.AddRange(existingProducts);
products.Add(new Product { Name="Hat", Price=10.25M });
products.Add(new Product { Name="Shirt", Price=20.95M });
dbcontext.BulkMerge(products);

BulkSync() - Performs a sync operation with a large number of entities.

By default any entities that do not exists in the source list will be deleted, but this can be disabled in the options.

var dbcontext = new MyDbContext();
var products = new List<Product>();
var existingProducts = dbcontext.Products.Where(o => o.Id <= 1000);
foreach(var product in existingProducts)
{
    product.Price = 6M;
}
products.AddRange(existingProducts);
products.Add(new Product { Name="Hat", Price=10.25M });
products.Add(new Product { Name="Shirt", Price=20.95M });
//All existing products with Id > 1000 will be deleted
dbcontext.BulkSync(products);

Fetch() - Retrieves data in batches.

var dbcontext = new MyDbContext();  
var query = dbcontext.Products.Where(o => o.Price < 5.35M);
query.Fetch(result =>
  {
    batchCount++;
    totalCount += result.Results.Count();
  }, 
  new FetchOptions { BatchSize = 1000 }
);
dbcontext.BulkUpdate(products);

DeleteFromQuery() - Deletes records from the database using a LINQ query without loading data in the context

var dbcontext = new MyDbContext(); 

//This will delete all products  
dbcontext.Products.DeleteFromQuery() 

//This will delete all products that are under $5.35  
dbcontext.Products.Where(x => x.Price < 5.35M).DeleteFromQuery()  

InsertFromQuery() - Inserts records from the database using a LINQ query without loading data in the context

var dbcontext = new MyDbContext(); 

//This will take all products priced under $10 from the Products table and 
//insert it into the ProductsUnderTen table
dbcontext.Products.Where(x => x.Price < 10M).InsertFromQuery("ProductsUnderTen", o => new { o.Id, o.Price });

UpdateFromQuery() - Updates records from the database using a LINQ query without loading data in the context

var dbcontext = new MyDbContext(); 

//This will change all products priced at $5.35 to $5.75 
dbcontext.Products.Where(x => x.Price == 5.35M).UpdateFromQuery(o => new Product { Price = 5.75M }) 

About

Bulk data support for the EntityFramework 6.4.4+

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%