OnTran.EF.SCU 1.0.0.2

dotnet add package OnTran.EF.SCU --version 1.0.0.2
                    
NuGet\Install-Package OnTran.EF.SCU -Version 1.0.0.2
                    
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="OnTran.EF.SCU" Version="1.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OnTran.EF.SCU" Version="1.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="OnTran.EF.SCU" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OnTran.EF.SCU --version 1.0.0.2
                    
#r "nuget: OnTran.EF.SCU, 1.0.0.2"
                    
#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.
#:package OnTran.EF.SCU@1.0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OnTran.EF.SCU&version=1.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=OnTran.EF.SCU&version=1.0.0.2
                    
Install as a Cake Tool

OnTran.EF.SCU

Logo

Use your EF-based code without any changes, without raw SQL, without breaking the transactionality and without application crashes due to OutOfMemory errors.
Minimal, Effective, multi-target EF Core extension for run short-lived contexts on the same connection and transaction when micro-ORM cannot be used.
Short-lived contexts will be used as the SQL formatter, without storing all inserted objects in the application memory due to the short lifetime of the scoped context.

Support DB types:
✅ MySql-based
✅ Postgres-based
✅ MS Sql

  • Other database types can be used, but testing was only done on these three.

Examples-Tests These tests on .net8.0 and use TestContainers to run DB in Docker.

If the functionality of this solution does not meet your needs, feel free to make your own version of this extension or just copy-paste code in your solution and change. It is open source.

Nuget

multi-target package:
✅ .net6.0
✅ .net7.0
✅ .net8.0
✅ .net9.0

https://www.nuget.org/packages/OnTran.EF.SCU

dotnet add package OnTran.EF.SCU

or

NuGet\Install-Package OnTran.EF.SCU

Example of using

using OnTran.EF.SCU;

await using var tran = await context.Database.BeginTransactionAsync(token);
//Get IServiceScopeFactory from DI to use this extension (via constructor of your class or via ServiceProvider).
var affected = await context.Database.ConstructScopedContextOnCurrentTranAsync<DBContext, int>(serviceScopeFactory, async (db, scope) =>
{
	// The 'db' instance from the functor is not equal to the 'context' instance. 'db' is a context instance without its own connection and transaction, it uses conn/tran from 'context'.
    for (var i = 0; i < 1000; i++)
	{
		db.TestEntities.Add(new TestEntity { Value = i.ToString() });
	}
    return await db.SaveChangesAsync(token);
}, token);
//all TestEntity will be gone (not immediately) from memory because 'db' context from the method above is disposed.
await context.Database.ConstructScopedContextOnCurrentTranAsync<DBContext>(serviceScopeFactory, async (db, scope) =>
{
    for (var i = 0; i < 1000; i++)
	{
		db.TestEntities.Add(new TestEntity { Value = i.ToString() });
	}
    await db.SaveChangesAsync(token);
}, token);
//all TestEntity will be gone (not immediately) from memory because 'db' context from the method above is disposed.
await context.Database.ConstructScopedContextOnCurrentTranAsync<DBContext>(serviceScopeFactory, async (db, scope) =>
{
    await scope.GetService<MyService>().ProcessManyRecordsAndInsertManyRecordsWithComplicatedLogicAsync(token);
	//DB context instance from DI in MyService instance from scope.GetService<MyService>() will use not own transaction/connetion, but you main tran/conn.
	//So, you can use any services via DI without code changes.
}, token);
//all TestEntity will be gone (not immediately) from memory because 'db' context from the method above is disposed.
await tran.CommitAsync(token);

License

Free MIT license (https://github.com/sapozhnikovv/OnTran.EF.SCU/blob/main/LICENSE)

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
1.0.0.2 124 2/1/2026
1.0.0.1 109 2/1/2026