CSharp · Console
// Entity Framework Extensions
// Doc: https://entityframework-extensions.net/bulk-update
// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
using (var context = new EntityContext())
{
context.Database.EnsureCreated();
}
SeedCustomers(5);
SeedInvoices(5, 3);
var customers = GenerateCustomers(3);
var invoices = GenerateInvoices(3, 3);
// Example
using (var context = new EntityContext())
{
FiddleHelper.WriteTable("1 - Customers Before", context.Customers.AsNoTracking());
FiddleHelper.WriteTable("2 - Invoices Before", context.Invoices.AsNoTracking());
FiddleHelper.WriteTable("3 - InvoiceItems Before", context.InvoiceItems.AsNoTracking());
// Easy to use
await context.BulkUpdateAsync(customers);
// Easy to customize
await context.BulkUpdateAsync(invoices, options => options.IncludeGraph = true);
FiddleHelper.WriteTable("4 - Customers After", context.Customers.AsNoTracking());
FiddleHelper.WriteTable("5 - Invoices After", context.Invoices.AsNoTracking());
FiddleHelper.WriteTable("6 - InvoiceItems After", context.InvoiceItems.AsNoTracking());
}
}
public static List<Customer> GenerateCustomers(int count, bool useOld = false)
{
List<Customer> list = new List<Customer>();
for(int i = 0; i < count; i++)
{
list.Add(new Customer() { CustomerID = (i + 1), Name = (useOld ? "Old_" : "") + "Customer_" + i});
}
return list;
}
public static List<Invoice> GenerateInvoices(int countInvoice, int countInvoiceItem, bool useOld = false)
{
List<Invoice> list = new List<Invoice>();
for(int i = 0; i < countInvoice; i++)
{
var invoice = new Invoice() { InvoiceID = (i + 1), Number = (useOld ? "Old_" : "") + "Number_" + i, Items = new List<InvoiceItem>() };
list.Add(invoice);
for(int j = 0; j < countInvoiceItem; j++)
{
invoice.Items.Add(new InvoiceItem() { InvoiceItemID = (i * countInvoiceItem) + (j + 1), ProductName = (useOld ? "Old_" : "") + "ProductName_" + i + "_" + j });
}
}
return list;
}
public static void SeedCustomers(int count)
{
var customers = GenerateCustomers(count, true);
new EntityContext().BulkInsert(customers);
}
public static void SeedInvoices(int countInvoice, int countInvoiceItem)
{
var invoices = GenerateInvoices(countInvoice, countInvoiceItem, true);
new EntityContext().BulkInsert(invoices, options => options.IncludeGraph = true);
}
}
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
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 DbSet<Invoice> Invoices { get; set; }
public DbSet<InvoiceItem> InvoiceItems { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
}
public class Invoice
{
public int InvoiceID { get; set; }
public string Number { get; set; }
public List<InvoiceItem> Items { get; set; }
}
public class InvoiceItem
{
public int InvoiceItemID { get; set; }
public string ProductName { get; set; }
public Invoice Invoice { get; set; }
}