Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions PhenX.EntityFrameworkCore.BulkInsert.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{45366E91-4
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhenX.EntityFrameworkCore.BulkInsert.MySql", "src\PhenX.EntityFrameworkCore.BulkInsert.MySql\PhenX.EntityFrameworkCore.BulkInsert.MySql.csproj", "{17649766-EA68-4333-8DA8-47B014A8B2CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhenX.EntityFrameworkCore.BulkInsert.Oracle", "src\PhenX.EntityFrameworkCore.BulkInsert.Oracle\PhenX.EntityFrameworkCore.BulkInsert.Oracle.csproj", "{98CC5F0A-5739-4570-A384-A3A067D09755}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{17649766-EA68-4333-8DA8-47B014A8B2CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17649766-EA68-4333-8DA8-47B014A8B2CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17649766-EA68-4333-8DA8-47B014A8B2CC}.Release|Any CPU.Build.0 = Release|Any CPU
{98CC5F0A-5739-4570-A384-A3A067D09755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98CC5F0A-5739-4570-A384-A3A067D09755}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98CC5F0A-5739-4570-A384-A3A067D09755}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98CC5F0A-5739-4570-A384-A3A067D09755}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -81,5 +87,6 @@ Global
{E4EB1C53-575C-45F8-924A-93DC42E8ACCA} = {F8A83782-311C-454D-8B97-B3FB86478BF4}
{450E859C-411F-4D67-A0B4-4E02C3D30E14} = {CBEBA2A8-79E0-412E-93C1-C88F4473D78B}
{17649766-EA68-4333-8DA8-47B014A8B2CC} = {CBEBA2A8-79E0-412E-93C1-C88F4473D78B}
{98CC5F0A-5739-4570-A384-A3A067D09755} = {CBEBA2A8-79E0-412E-93C1-C88F4473D78B}
EndGlobalSection
EndGlobal
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PhenX.EntityFrameworkCore.BulkInsert

A high-performance, provider-agnostic bulk insert extension for Entity Framework Core 8+. Supports SQL Server, PostgreSQL, SQLite and MySQL.
A high-performance, provider-agnostic bulk insert extension for Entity Framework Core 8+. Supports SQL Server, PostgreSQL, SQLite, MySQL and Oracle.

Its main purpose is to provide a fast way to perform simple bulk inserts in Entity Framework Core applications.

Expand All @@ -21,6 +21,7 @@ but they are in [the roadmap](#roadmap).
| `PhenX.EntityFrameworkCore.BulkInsert.PostgreSql` | For PostgreSQL | [![NuGet](https://img.shields.io/nuget/v/PhenX.EntityFrameworkCore.BulkInsert.PostgreSql.svg)](https://www.nuget.org/packages/PhenX.EntityFrameworkCore.BulkInsert.PostgreSql) |
| `PhenX.EntityFrameworkCore.BulkInsert.Sqlite` | For SQLite | [![NuGet](https://img.shields.io/nuget/v/PhenX.EntityFrameworkCore.BulkInsert.Sqlite.svg)](https://www.nuget.org/packages/PhenX.EntityFrameworkCore.BulkInsert.Sqlite) |
| `PhenX.EntityFrameworkCore.BulkInsert.MySql` | For MySql | [![NuGet](https://img.shields.io/nuget/v/PhenX.EntityFrameworkCore.BulkInsert.Sqlite.svg)](https://www.nuget.org/packages/PhenX.EntityFrameworkCore.BulkInsert.MySql) |
| `PhenX.EntityFrameworkCore.BulkInsert.Oracle` | For Oracle | [![NuGet](https://img.shields.io/nuget/v/PhenX.EntityFrameworkCore.BulkInsert.Oracle.svg)](https://www.nuget.org/packages/PhenX.EntityFrameworkCore.BulkInsert.Oracle) |
| `PhenX.EntityFrameworkCore.BulkInsert` | Common library | [![NuGet](https://img.shields.io/nuget/v/PhenX.EntityFrameworkCore.BulkInsert.svg)](https://www.nuget.org/packages/PhenX.EntityFrameworkCore.BulkInsert) |

## Installation
Expand All @@ -39,6 +40,9 @@ Install-Package PhenX.EntityFrameworkCore.BulkInsert.Sqlite

# For MySql
Install-Package PhenX.EntityFrameworkCore.BulkInsert.MySql

# For Oracle
Install-Package PhenX.EntityFrameworkCore.BulkInsert.Oracle
```

## Usage
Expand All @@ -58,6 +62,8 @@ services.AddDbContext<MyDbContext>(options =>
.UseBulkInsertSqlite()
// OR
.UseBulkInsertMySql()
// OR
.UseBulkInsertOracle()
;
});
```
Expand Down Expand Up @@ -183,6 +189,10 @@ MySQL results with 500 000 rows :

![bench-mysql.png](https://raw.githubusercontent.com/PhenX/PhenX.EntityFrameworkCore.BulkInsert/refs/heads/main/images/bench-mysql.png)

Oracle results with 500 000 rows :

![bench-oracle.png](https://raw.githubusercontent.com/PhenX/PhenX.EntityFrameworkCore.BulkInsert/refs/heads/main/images/bench-oracle.png)

## Contributing

Contributions are welcome! Please open issues or submit pull requests for bug fixes, features, or documentation improvements.
Expand Down
Binary file added images/bench-oracle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Oracle.ManagedDataAccess.Client;

using PhenX.EntityFrameworkCore.BulkInsert.Options;

namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle;

/// <summary>
/// Options specific to Oracle bulk insert.
/// </summary>
public class OracleBulkInsertOptions : BulkInsertOptions
{
/// <inheritdoc cref="OracleBulkCopyOptions"/>
public OracleBulkCopyOptions CopyOptions { get; set; } = OracleBulkCopyOptions.Default;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using JetBrains.Annotations;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

using Oracle.ManagedDataAccess.Client;

using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;

namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle;

[UsedImplicitly]
internal class OracleBulkInsertProvider(ILogger<OracleBulkInsertProvider>? logger) : BulkInsertProviderBase<OracleDialectBuilder, OracleBulkInsertOptions>(logger)
{
/// <inheritdoc />
protected override string BulkInsertId => "ROWID";

/// <inheritdoc />
protected override string AddTableCopyBulkInsertId => ""; // No need to add an ID column in Oracle

/// <inheritdoc />
/// <summary>
/// The temporary table name is generated with a GUID to ensure uniqueness, but limited to less than 30 characters,
/// because Oracle prior 12.2 has a limit of 30 characters for identifiers.
/// </summary>
protected override string GetTempTableName(string tableName) => $"#temp_bulk_insert_{Guid.NewGuid().ToString("N")[..8]}";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you not take the whole guid?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oracle under a certain version (12 I think) does not allow indentifiers longer than 30, yay

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably comment that, because I would have forgotten that next week.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I will remember 😅 but yeah I'll add a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ahve said this very often to myself :D


protected override OracleBulkInsertOptions CreateDefaultOptions() => new()
{
BatchSize = 50_000,
};

/// <inheritdoc />
protected override IAsyncEnumerable<T> BulkInsertReturnEntities<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
OracleBulkInsertOptions options,
OnConflictOptions<T>? onConflict,
CancellationToken ctk)
{
throw new NotSupportedException("Provider does not support returning entities.");
}

/// <inheritdoc />
protected override Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
string tableName,
IReadOnlyList<ColumnMetadata> columns,
OracleBulkInsertOptions options,
CancellationToken ctk)
{
var connection = (OracleConnection) context.Database.GetDbConnection();

using var bulkCopy = new OracleBulkCopy(connection, options.CopyOptions);

bulkCopy.DestinationTableName = tableInfo.QuotedTableName;
bulkCopy.BatchSize = options.BatchSize;
bulkCopy.BulkCopyTimeout = options.GetCopyTimeoutInSeconds();

foreach (var column in columns)
{
bulkCopy.ColumnMappings.Add(column.PropertyName, column.QuotedColumName);
}

var dataReader = new EnumerableDataReader<T>(entities, columns, options);

bulkCopy.WriteToServer(dataReader);

return Task.CompletedTask;
}

/// <inheritdoc />
protected override async Task DropTempTableAsync(bool sync, DbContext dbContext, string tableName)
{
var commandText = $"""
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE {tableName}';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN -- ORA-00942: table or view does not exist
RAISE;
END IF;
END;
""";

await ExecuteAsync(sync, dbContext, commandText, CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;

using PhenX.EntityFrameworkCore.BulkInsert.Extensions;

namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle;

/// <summary>
/// DbContext options extension for Oracle.
/// </summary>
public static class OracleDbContextOptionsExtensions
{
/// <summary>
/// Configures the DbContext to use the Oracle bulk insert provider.
/// </summary>
public static DbContextOptionsBuilder UseBulkInsertOracle(this DbContextOptionsBuilder optionsBuilder)
{
return optionsBuilder.UseProvider<OracleBulkInsertProvider>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Text;

using Microsoft.EntityFrameworkCore;

using PhenX.EntityFrameworkCore.BulkInsert.Dialect;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;

namespace PhenX.EntityFrameworkCore.BulkInsert.Oracle;

internal class OracleDialectBuilder : SqlDialectBuilder
{
protected override string OpenDelimiter => "\"";
protected override string CloseDelimiter => "\"";
protected override string ConcatOperator => "||";

protected override bool SupportsMoveRows => false;

public override string CreateTableCopySql(string tempTableName, TableMetadata tableInfo, IReadOnlyList<ColumnMetadata> columns)
{
return CreateTableCopySqlBase(tempTableName, columns);
}

public override string BuildMoveDataSql<T>(
DbContext context,
TableMetadata target,
string source,
IReadOnlyList<ColumnMetadata> insertedColumns,
IReadOnlyList<ColumnMetadata> returnedColumns,
BulkInsertOptions options,
OnConflictOptions? onConflict = null)
{
var q = new StringBuilder();

// Merge handling
if (onConflict is OnConflictOptions<T> onConflictTyped)
{
IEnumerable<string> matchColumns;
if (onConflictTyped.Match != null)
{
matchColumns = GetColumns(target, onConflictTyped.Match);
}
else if (target.PrimaryKey.Count > 0)
{
matchColumns = target.PrimaryKey.Select(x => x.QuotedColumName);
}
else
{
throw new InvalidOperationException("Table has no primary key that can be used for conflict detection.");
}

q.AppendLine($"MERGE INTO {target.QuotedTableName} AS {PseudoTableInserted}");

q.Append("USING (SELECT ");
q.AppendColumns(insertedColumns);
q.Append($" FROM {source}) AS {PseudoTableExcluded} (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");

q.Append("ON ");
q.AppendJoin(" AND ", matchColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col} = {PseudoTableExcluded}.{col}"));
q.AppendLine();

if (onConflictTyped.Update != null)
{
var columns = target.GetColumns(false);

q.AppendLine("WHEN MATCHED THEN UPDATE SET ");
q.AppendJoin(", ", GetUpdates(context, target, columns, onConflictTyped.Update));
q.AppendLine();
}

q.Append("WHEN NOT MATCHED THEN INSERT (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");

q.Append("VALUES (");
q.AppendJoin(", ", insertedColumns, (b, col) => b.Append($"{PseudoTableExcluded}.{col.QuotedColumName}"));
q.AppendLine(")");

if (returnedColumns.Count != 0)
{
q.Append("OUTPUT ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($"{PseudoTableInserted}.{col.QuotedColumName} AS {col.QuotedColumName}"));
q.AppendLine();
}
}

// No conflict handling
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps two methods so that the comments are not necessary?

else
{
q.Append($"INSERT INTO {target.QuotedTableName} (");
q.AppendColumns(insertedColumns);
q.AppendLine(")");
q.Append("SELECT ");
q.AppendColumns(insertedColumns);
q.AppendLine();
q.Append($"FROM {source}");
q.AppendLine();

if (returnedColumns.Count != 0)
{
q.Append("RETURNING ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append(col.QuotedColumName));
q.Append(" INTO ");
q.AppendJoin(", ", returnedColumns, (b, col) => b.Append($":{col.ColumnName}"));
q.AppendLine();
}
}

q.AppendLine(";");

return q.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\PhenX.EntityFrameworkCore.BulkInsert\PhenX.EntityFrameworkCore.BulkInsert.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Oracle.EntityFrameworkCore" Condition="'$(TargetFramework)' == 'net8.0'" Version="8.23.80" />
<PackageReference Include="Oracle.EntityFrameworkCore" Condition="'$(TargetFramework)' == 'net9.0'" Version="9.23.80" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,7 @@ internal class SqlServerDialectBuilder : SqlDialectBuilder

public override string CreateTableCopySql(string tempTableName, TableMetadata tableInfo, IReadOnlyList<ColumnMetadata> columns)
{
var q = new StringBuilder();
q.Append($"CREATE TABLE {tempTableName} (");

foreach (var column in columns)
{
q.Append($"{column.QuotedColumName} {column.StoreDefinition}");
if (column != columns[^1])
{
q.Append(',');
}
q.AppendLine();
}

q.AppendLine(")");

return q.ToString();
return CreateTableCopySqlBase(tempTableName, columns);
}

protected override string Trim(string lhs) => $"TRIM({lhs})";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class SqliteBulkInsertProvider(ILogger<SqliteBulkInsertProvider>? logge
protected override string AddTableCopyBulkInsertId => "--"; // No need to add an ID column in SQLite

/// <inheritdoc />
protected override string GetTempTableName(string tableName) => $"_temp_bulk_insert_test_entity_{Guid.NewGuid():N}";
protected override string GetTempTableName(string tableName) => $"_temp_bulk_insert_{Guid.NewGuid():N}";

/// <inheritdoc />
protected override BulkInsertOptions CreateDefaultOptions() => new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ protected virtual async Task AddBulkInsertIdColumn<T>(
string tempTableName,
CancellationToken ctk) where T : class
{
if (string.IsNullOrEmpty(AddTableCopyBulkInsertId))
{
// No need to add an ID column in this provider
return;
}

var alterQuery = string.Format(AddTableCopyBulkInsertId, tempTableName);

await ExecuteAsync(sync, context, alterQuery, ctk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ internal abstract class SqlDialectBuilder
/// </summary>
protected virtual bool SupportsInsertIntoAlias => true;

protected static string CreateTableCopySqlBase(string tempTableName, IReadOnlyList<ColumnMetadata> columns)
{
var q = new StringBuilder();

q.Append($"CREATE TABLE {tempTableName} (");
q.AppendJoin(",", columns, (sb, column) => sb.AppendLine($"{column.QuotedColumName} {column.StoreDefinition}"));
q.AppendLine(")");

return q.ToString();
}

public abstract string CreateTableCopySql(string tempNameName, TableMetadata tableInfo, IReadOnlyList<ColumnMetadata> columns);

/// <summary>
Expand Down
Loading