Tanneryd.BulkOperations.EF6 1.2.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tanneryd.BulkOperations.EF6 --version 1.2.5                
NuGet\Install-Package Tanneryd.BulkOperations.EF6 -Version 1.2.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tanneryd.BulkOperations.EF6" Version="1.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tanneryd.BulkOperations.EF6 --version 1.2.5                
#r "nuget: Tanneryd.BulkOperations.EF6, 1.2.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tanneryd.BulkOperations.EF6 as a Cake Addin
#addin nuget:?package=Tanneryd.BulkOperations.EF6&version=1.2.5

// Install Tanneryd.BulkOperations.EF6 as a Cake Tool
#tool nuget:?package=Tanneryd.BulkOperations.EF6&version=1.2.5                

Tanneryd.BulkOperations.EF6

A nuget package that extends the DbContext in EF6 with bulk operations for both inserts and updates.

Getting Started

Background

Read the CodeProject article Bulk operations using Entity Framework if you are interested in some background.

Prerequisites

The extension is built for, and requires, Entity Framework 6 and .NET 4.5 or later.

Installing

Install the nuget package Tanneryd.BulkOperations.EF6. This will make the following methods available on the DbContext.

API

Insert
public class BulkInsertRequest<T>
{
    public IList<T> Entities { get; set; }
    public SqlTransaction Transaction { get; set; }
    public bool UpdateStatistics { get; set; } = false;
    public bool Recursive { get; set; } = false;
    public bool AllowNotNullSelfReferences { get; set; } = false;
    public bool SortUsingClusteredIndex { get; set; } = true;
}
  • When UpdateStatistics is set the command "UPDATE STATISTICS <tablename> WITH ALL" will be executed after the insert.
  • When Recursive is set to true the entire entity hierarchy will be inserted.
  • When AllowNotNullSelfReferences is set to true, entities with self referencing foreign keys declared as NOT NULL will be properly inserted. But, this will only work if the database user has the required privileges to execute ALTER TABLE <table name> NOCHECK CONSTRAINT ALL and ALTER TABLE <table name> CHECK CONSTRAINT ALL.
  • When SortUsingClusteredIndex is set to true the entities will be sorted according to the clustered index of the target table.
 public static BulkOperationResponse BulkInsertAll<T>(
     this DbContext ctx,
     BulkInsertRequest<T> request)
Update
public class BulkUpdateRequest
{
    public IList Entities { get; set; }
    public string[] UpdatedColumnNames { get; set; }
    public string[] KeyMemberNames { get; set; }
    public SqlTransaction Transaction { get; set; }
    public bool InsertIfNew { get; set; }
}
  • If UpdatedColumnNames is an empty list all non-key mapped columns will be updated, otherwise only the columns specified.
  • If KeyMemberNames is an empty list the primary key columns will be used to select which rows to update, otherwise the columns specified will be used.
 public static BulkOperationResponse BulkUpdateAll(
     this DbContext ctx,
     BulkUpdateRequest request)
Select
Select
SelectExisting

The select-existing feature provides a way to identify the subset of existing or non-existing items in a local collection where an item is considered as existing if it is equal to an entity saved in the database according to a set of defined key properties. This provides a very efficient way of figuring out which items in your local collection needs to be inserted and which to be updated. The item collection can be of the same type as the EF entity but it does not have to be.

public class BulkSelectRequest<T>
{
	public BulkSelectRequest(string[] keyPropertyNames, 
				 IList<T> items = null,
				 SqlTransaction transaction = null)
	{
        KeyPropertyMappings = keyPropertyNames.Select(n => new KeyPropertyMapping
            {
                ItemPropertyName = n,
                EntityPropertyName = n
            })
            .ToArray();
        Items = items;
        Transaction = transaction;
    }
    public IList<T> Items { get; set; }
    public KeyPropertyMapping[] KeyPropertyMappings { get; set; }
    public SqlTransaction Transaction { get; set; }
   

    public BulkSelectRequest()
    {
        KeyPropertyMappings = new KeyPropertyMapping[0];
        Items = new T[0];
    }
}

public class KeyPropertyMapping
{
    public string ItemPropertyName { get; set; }
    public string EntityPropertyName { get; set; }

    public static KeyPropertyMapping[] IdentityMappings(string[] names)
    {
        return names.Select(n => new KeyPropertyMapping
        {
            ItemPropertyName = n,
            EntityPropertyName = n
        }).ToArray();
    }
}
/// <summary>
/// Given a set of entities we return the subset of these entities
/// that already exist in the database, according to the key selector
/// used.
/// </summary>
/// <typeparam name="T1">The item collection type</typeparam>
/// <typeparam name="T2">The EF entity type</typeparam>
/// <param name="ctx"></param>
/// <param name="request"></param>
public static IList<T1> BulkSelectExisting<T1,T2>(
    this DbContext ctx,
    BulkSelectRequest<T1> request)
Delete
DeleteExisting
DeleteNotExisting
        /// <summary>
        /// The bulk delete request contains a SqlCondition. It has
        /// a list of column name/column value pairs and will be used
        /// to build an AND where clause. This method will delete any
        /// rows in the database that matches this SQL condition unless
        /// it also matches one of the supplied entities according to
        /// the key selector used.
        ///
        /// !!! IMPORTANT !!!
        /// MAKE SURE THAT YOU FULLY UNDERSTAND THIS LOGIC
        /// BEFORE USING THE BULK DELETE METHOD SO THAT YOU
        /// DO NOT END UP WITH AN EMPTY DATABASE.
        /// 
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="ctx"></param>
        /// <param name="request"></param>
        public static void BulkDeleteNotExisting<T1, T2>(
            this DbContext ctx,
            BulkDeleteRequest<T1> request)

Release history

1.2.5 (2019-07-15)
  • Bugfix: Issue #18 - Bug when parsing table names.
  • Bugfix: Sorting on a clustered index did not work for tables with schemas.
  • Bugfix: BulkSelectExisting and BulkSelectNotExisting sometimes returned duplicates.
1.2.4 (2019-05-26)
  • Bugfix: Issue #16 - BulkInsert is not thread safety.
1.2.3 (2019-03-29)
  • Bugfix: Join tables with Guid keys misbehaved.
  • Added method BulkDeleteNotExisting
1.2.2 (2018-12-01)
  • Bugfix: BulkSelect did not work properly with null columns.
  • Bugfix: Contexts using lazy loading and thus dynamic proxies did not work as expected.
  • Bugfix: Tables with Guid primary keys did not work as expected in some situations.

Built With

  • Visual Studio 2019
  • .NET Framework 4.5
  • Entity Framework 6.2.0

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Måns Tånneryd

License

This project is licensed under the Apache License - see the LICENSE.md file for details.

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.1 426 8/15/2024
2.1.0 1,023 7/3/2024
2.0.4 8,399 9/5/2023
1.4.1 41,802 11/12/2021
1.4.0 107,843 6/15/2020
1.4.0-experimental 3,254 1/14/2020
1.4.0-beta2 368 5/10/2020
1.4.0-beta1 844 4/1/2020
1.3.1 28,320 1/3/2020
1.3.1-beta1 395 12/30/2019
1.3.0 686 12/21/2019
1.3.0-beta1 404 11/6/2019
1.2.7-rc4 613 10/12/2019
1.2.7-rc3 407 9/23/2019
1.2.7-rc2 405 9/11/2019
1.2.7-rc1 398 9/10/2019
1.2.5 20,852 7/15/2019
1.2.5-beta1 483 6/12/2019
1.2.4 1,578 5/26/2019
1.2.3 5,879 3/29/2019
1.2.3-beta6 7,016 2/12/2019
1.2.3-beta5 518 2/10/2019
1.2.3-beta4 523 2/9/2019
1.2.3-beta3 535 2/2/2019
1.2.3-beta2 734 1/16/2019
1.2.3-beta1 560 1/13/2019
1.2.2 14,353 12/1/2018
1.2.1 7,462 9/24/2018
1.2.1-rc2 611 9/17/2018
1.2.1-rc1 653 9/12/2018
1.2.0 1,422 9/5/2018
1.2.0-rc2 665 8/24/2018
1.2.0-rc1 712 8/16/2018
1.2.0-beta5 942 7/11/2018
1.2.0-beta4 751 6/12/2018
1.2.0-beta3 937 5/12/2018
1.2.0-beta2 1,013 5/2/2018
1.2.0-beta1 755 4/23/2018
1.1.0 8,017 4/20/2018
1.1.0-beta7 760 4/18/2018
1.1.0-beta6 789 4/16/2018
1.1.0-beta5 932 4/13/2018
1.1.0-beta4 827 4/13/2018
1.1.0-beta 978 4/12/2018

Bugfix: Issue #18 - Bug When Parsing Table Names. Sorting on a clustered index did not work for tables with schemas.