Last active
January 12, 2017 03:02
-
-
Save JonathanMagnan/6e98148debc5fd79527cc6de5e52847f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var ctx = new CustomerContext()) | |
{ | |
foreach(var line in lines) | |
{ | |
var customer = new Customer(); | |
// ...code... | |
ctx.Customers.Add(customer); | |
} | |
ctx.SaveChanges(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var ctx = new CustomerContext()) | |
{ | |
// 1. CREATE a list | |
List<Customer> customers = new List<Customer>(); | |
foreach(var line in lines) | |
{ | |
var customer = new Customer(); | |
// ...code... | |
// 2. ADD entity to the list | |
customers.Add(customer); | |
} | |
// 3. USE AddRange with the list | |
ctx.Customers.AddRange(customers); | |
// 4. SaveChanges | |
ctx.SaveChanges(); | |
// 5. Done! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var ctx = new CustomerContext()) | |
{ | |
// 1. SET AutoDetectChangesEnabled = false | |
ctx.Configuration.AutoDetectChangesEnabled = false; | |
foreach(var line in lines) | |
{ | |
var customer = new Customer(); | |
// ...code... | |
ctx.Customers.Add(customer); | |
} | |
// 2. CALL DetectChanges before SaveChanges | |
ctx.ChangeTracker.DetectChanges(); | |
// 3. SaveChanges | |
ctx.SaveChanges(); | |
// 4. Done! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. CREATE a batchSize variable | |
int batchSize = 1000; | |
var ctx = new CustomerContext(); | |
for (int i = 0; i < lines.Count; i++) | |
{ | |
// 2. CALL SaveChanges before creating a new batch | |
if (i != 0 && i%batchSize == 0) | |
{ | |
ctx.SaveChanges(); | |
ctx = new CustomerContext(); | |
} | |
var customer = new Customer(); | |
// ...code... | |
ctx.Customers.Add(customer); | |
} | |
// 3. CALL SaveChanges | |
ctx.SaveChanges(); | |
// 4. Done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment