Last active
January 12, 2017 03:02
-
-
Save JonathanMagnan/d1eadcc815d4a4af8c397e16e98ab32e 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... | |
// DetectChanges is invoked every time you call Add | |
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
// 1. CREATE 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! |
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
using (var ctx = new EF6.CustomerContext()) | |
{ | |
for(int i = 0; i < lines.Count; i++) | |
{ | |
ctx.Customers.Add(customer); | |
} | |
} |
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
internal virtual void DetectChanges() | |
{ | |
IList<EntityEntry> entityEntriesForDetectChanges = this.GetEntityEntriesForDetectChanges(); | |
if ((entityEntriesForDetectChanges != null) && this.TransactionManager.BeginDetectChanges()) | |
{ | |
try | |
{ | |
DetectChangesInNavigationProperties(entityEntriesForDetectChanges); | |
DetectChangesInScalarAndComplexProperties(entityEntriesForDetectChanges); | |
DetectChangesInForeignKeys(entityEntriesForDetectChanges); | |
this.DetectConflicts(entityEntriesForDetectChanges); | |
this.TransactionManager.BeginAlignChanges(); | |
this.AlignChangesInRelationships(entityEntriesForDetectChanges); | |
} | |
finally | |
{ | |
this.TransactionManager.EndAlignChanges(); | |
this.TransactionManager.EndDetectChanges(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment