Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanMagnan/ccb5d5c4f4c71a5b5bc10d3de5a4903a to your computer and use it in GitHub Desktop.
Save JonathanMagnan/ccb5d5c4f4c71a5b5bc10d3de5a4903a to your computer and use it in GitHub Desktop.
EFE - Bulk-Update - Entity Graph
// Description: EFE - Bulk-Update - Entity Graph
// Website: https://entityframework-extensions.net/
// Run: https://dotnetfiddle.net/02Mjoy
// @nuget: EntityFramework
// @nuget: EntityFramework.SqlServerCompact
// @nuget: Microsoft.SqlServer.Compact
// @nuget: Z.EntityFramework.Extensions
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// Custom configuration specific for .NET Fiddle
Z.EntityFramework.Extensions.EntityFrameworkManager.UseFiddleSqlCompact(System.Data.Entity.SqlServerCompact.SqlCeProviderServices.Instance, System.Data.SqlServerCe.SqlCeProviderFactory.Instance);
GenerateData();
List<Invoice> invoices;
using (var context = new EntityContext())
{
invoices = context.Invoices.Include(x => x.InvoiceItems).ToList();
}
invoices.ForEach(x => {x.Description = x.Description.Replace("Invoice", "Updated"); x.InvoiceItems.ForEach(y => y.Description = y.Description.Replace("Invoice", "Updated"));});
// BulkUpdate: INCLUDE child entities
using (var context = new EntityContext())
{
context.BulkUpdate(invoices, options => options.IncludeGraph = true);
}
using (var context = new EntityContext())
{
FiddleHelper.WriteTable(context.Invoices.ToList().Select(x => new { x.InvoiceID, x.Description}));
FiddleHelper.WriteTable(context.InvoiceItems.ToList());
}
}
public static void GenerateData()
{
List<Invoice> list = new List<Invoice>() {
new Invoice() { Description = "Invoice_A", InvoiceItems = new List<InvoiceItem>() { new InvoiceItem() { Description = "Invoice_A_InvoiceItem_A" } , new InvoiceItem() { Description = "Invoice_A_InvoiceItem_B" }}},
new Invoice() { Description = "Invoice_B", InvoiceItems = new List<InvoiceItem>() { new InvoiceItem() { Description = "Invoice_B_InvoiceItem_A" } , new InvoiceItem() { Description = "Invoice_B_InvoiceItem_B" }}}};
// BulkInsert: INCLUDE child entities
using (var context = new EntityContext())
{
context.BulkInsert(list, options => options.IncludeGraph = true);
}
}
public class EntityContext : DbContext
{
public EntityContext() : base(@"Data Source=ZZZ_Projects.sdf")
{
}
public DbSet<Invoice> Invoices { get; set; }
public DbSet<InvoiceItem> InvoiceItems { get; set; }
}
public class Invoice
{
public int InvoiceID { get; set; }
public string Description { get; set; }
public List<InvoiceItem> InvoiceItems { get; set; }
}
public class InvoiceItem
{
public int InvoiceItemID { get; set; }
public string Description { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment