CSharp · Console
// Entity Framework Classic
// Doc: https://entityframework-classic.net/bulk-insert
// @nuget: Z.EntityFramework.Classic
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Entity;
public class Program
{
public static void Main()
{
var customers = GenerateCustomers(10);
using (var context = new EntityContext())
{
FiddleHelper.WriteTable("1 - Customers Before", context.Customers);
context.BulkInsert(customers, options => {
options.ColumnInputExpression = x => new { x.Code, x.CreatedDate };
});
FiddleHelper.WriteTable("2 - Customers After", context.Customers);
}
}
public static List<Customer> GenerateCustomers(int count)
{
var customers = new List<Customer>();
for(int i = 0; i < count; i++)
{
customers.Add(new Customer() { Code = "Inserted_" + i, Name = "Skipped_" + i, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now });
}
return customers;
}
public class EntityContext : DbContext
{
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
}
}