EF Extensions - BulkSynchronize - Getting Started - Synchronizing with options (Entity) (NET Core)

CSharp · Console

/Program.cs

// Bulk Operations
// Doc: https://bulk-operations.net/bulk-synchronize

// @nuget: Z.BulkOperations
// @nuget: System.Data.SqlClient

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Z.BulkOperations;
				
public class Program
{
	public static void Main()
	{
		GenerateDatabase();
		
		GenerateCustomers(3);
		
		var customizeToSynchronize = new List<Customer>();
		
		for(int i = 0; i < 2; i++)
		{
			customizeToSynchronize.Add(new Customer() { Code = "Code_" + i, FirstName = "Updated_FirstName_" + i, LastName = "Updated_LastName_" + i });
		}
		
		for(int i = 0; i < 2; i++)
		{
			customizeToSynchronize.Add(new Customer() { Code = "New_Code_" + i, FirstName = "New_FirstName_" + i, LastName = "New_LastName_" + i });
		}
		
		using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
		{
			connection.Open();
			
			// SHOW data
			ShowData(connection, "Before");
			
			// SAVE customers
			using (var bulk = new BulkOperation<Customer>(connection))
			{
				bulk.DestinationTableName = "Customers";

				bulk.AutoMapKeyExpression = customer => customer.Code;
				bulk.BulkSynchronize(customizeToSynchronize);
			}
			
			// SHOW data
			ShowData(connection, "After");
		}
	}
	
	public static void ShowData(SqlConnection connection, string text)
	{
		// SHOW data
		using (var command = connection.CreateCommand())
		{
			command.CommandText = @"
SELECT * FROM Customers
";
			var ds = new DataSet();
			var sqlDataAdapter = new SqlDataAdapter(command);
			sqlDataAdapter.Fill(ds);

			ds.Tables[0].TableName = "1 - Customers " + text;

			FiddleHelper.WriteTable(ds.Tables[0]);
		}
	}
	
	public static void GenerateCustomers(int count) 
	{
		List<Customer> list = new List<Customer>();
		
		for(int i = 0; i < count; i++)
		{
			list.Add(new Customer() { Code = "Code_" + i, FirstName = "FirstName_" + i, LastName = "LastName_" + i });
		}
		
		using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
		{
			connection.Open();
			
			// SAVE customers
			using (var bulk = new BulkOperation<Customer>(connection))
			{
				// easy to use
				bulk.DestinationTableName = "Customers";

				bulk.BulkInsert(list);
			}
		}
	}
	
	public static void GenerateDatabase()
	{
		using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
		{
			connection.Open();
			using (var command = connection.CreateCommand())
			{
				command.CommandText = @"   
CREATE TABLE [Customers]
(
	[CustomerID] [INT] IDENTITY(1,1) NOT NULL,
	[Code] [NVARCHAR](MAX) NULL,
	[FirstName] [NVARCHAR](MAX) NULL,
	[LastName] [NVARCHAR](MAX) NULL,
	CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
	(
		[CustomerID] ASC
	)
)";
				command.ExecuteNonQuery();
			}
		}
	}
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public string Code { get; set; }
		public string FirstName { get; set; }
		public string LastName { get; set; }
	}
}
;