EFE - Temporary Table - TemporaryTableMinRecord (EF Core)

CSharp · Console

/Program.cs

// Entity Framework Extensions
// Doc: https://entityframework-extensions.net/temporary-table#temporarytableminrecord

// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore

/*
## 🏷️ TemporaryTableMinRecord

Specifies the **minimum number of records** required before the library creates and uses a temporary table.

If the number of records in your bulk operation is **below this threshold**, the library will automatically use an **inline table** instead of creating a staging table.

This helps avoid the overhead of creating and dropping temporary tables when dealing with very small datasets.

By default, the minimum is **20 records**.

The default value is intentionally low because SQL Server has a **maximum parameter limit**, and inline tables are only efficient for small sets of values.

### 💡 Why it can be useful

* **Faster tiny operations** → Inline tables are quicker when only a handful of rows are involved.
* **Avoids unnecessary overhead** → No need to create/drop a temp table for fewer than N records.
* **Optimized defaults** → Default value of 20 ensures inline tables are only used where they’re truly efficient.
* **Fine-tuned performance** → You can adjust the threshold depending on your workload (e.g., raise it if you frequently process medium-sized sets).

### ⚠️ Notes & gotchas

* Inline tables rely on SQL parameters, so they are only practical for **small record counts**.
* If you set the threshold too high, you may hit SQL Server’s maximum parameter limitation.
* For most scenarios, the default of **20** provides the best balance.
*/

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;

public class Program
{
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		
		using (var context = new EntityContext())
		{
			context.Customers.AddRange(AddToList());
			
			context.BulkSaveChanges(options =>
			{
				options.TemporaryTableMinRecord = 25;
			});
		}
		
		using (var context = new EntityContext())
		{
			FiddleHelper.WriteTable(context.Customers.Take(5).ToList());	
		}
	}
		 	
	public static List<Customer> AddToList()
	{
		var list = new List<Customer>();
		for (int i = 0; i < 20; i++)
		{
			list.Add(new Customer() { Name ="Customer_" + i});
		}
		
		return list;
	}

	public class EntityContext : DbContext
	{
		public EntityContext() 
		{
		}

		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));

			base.OnConfiguring(optionsBuilder);
		}
		
		public DbSet<Customer> Customers { get; set; }
	}
	
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public Boolean IsActive { get; set; }
	}
}
;