EFE - Temporary Table - TemporaryTablePersist (EF Core)

CSharp · Console

/Program.cs

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

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

/*
## 🏷️ TemporaryTablePersist

Keeps the data in the temporary table after the bulk operation is finished.

By default, when the library creates a temporary table automatically, the table (and its data) is dropped at the end of the operation.
When you provide a custom `TemporaryTableName`, the library also assumes it can safely drop it unless told otherwise.

Setting `TemporaryTablePersist = true;` prevents this cleanup and ensures that the temporary table and its data remain available for as long as the **database connection remains open**.

### 💡 Why it can be useful

* **Reuse populated data** → Run multiple bulk operations (insert, update, merge, delete) against the same temporary table without reloading the data.
* **Improve performance** → Avoids the overhead of re-creating and re-filling the staging table for each step of a process.
* **Chained operations** → Ideal when several queries in sequence need to work on the same temporary dataset.
* **Debugging & inspection** → Keep the data visible in the temp table for troubleshooting during the session.

⚠️ **Important note**: Temporary tables and their data **only persist while the connection is open**.
Once the connection is closed or disposed, SQL Server automatically drops the table and clears its contents.
*/

using System;
using System.Linq;
using System.Text;
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())
		{
			// minimum 11 customers to use a temporary table
			var customers = GenerateCustomer(11);

			context.Database.OpenConnection();
			
			context.BulkMerge(customers, options =>
			{
				options.TemporaryTableName = "#MyTemporaryTableName";
				options.TemporaryTableCreate = true;
				options.TemporaryTablePersist = true;
			});
			
			// You can now reuse or query "#MyTemporaryTableName" here
			
			context.Database.CloseConnection();
			
			FiddleHelper.WriteTable(context.Customers.AsNoTracking());
		}
	}
		 	
	public static List<Customer> GenerateCustomer(int max)
	{
		var list = new List<Customer>();
		for (int i = 0; i < max; 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; }
	}
}
;