static void Main(string[] args) { Console.WriteLine("Init database..."); MovieTheaterRatingContext context = new MovieTheaterRatingContext(); //***** UNCOMMENT THESE OUT TO POPULATE DATABASE *********// context.Database.Initialize(true); if (context != null) context.Dispose(); //AddManyRecords(); //UNCOMMENT THIS OUT IF WANNA TRY WITH BULK INSERT Console.WriteLine("Done..."); //Console.WriteLine("==== Calling TestProcessTAskAsync() first ===="); //TestProcessTAskAsync(); //Console.WriteLine("press any key to exit"); //Console.WriteLine("==== Calling LongRunningMethod() second ===="); //TestAsyncAwaitMethods(); //Console.WriteLine("press any key to exit"); //Console.WriteLine("==== Calling TestLoop() third ===="); //TestLoop(); //Console.WriteLine("press any key to exit"); //Console.WriteLine("==== Calling AsyncAwaitDemo class fourth ===="); //var demo = new AsyncAwaitDemo(); //demo.Test(); //Console.WriteLine("==== Calling AsyncAwaitDemo2 class fourth ===="); //var demo2 = new AsyncAwaitDemo2(); //demo2.TestLoop(); Console.ReadLine(); }
private static MovieTheaterRatingContext AddToContext(MovieTheaterRatingContext db, Movie entity, int count, int commitCount, bool recreatedContext) { db.Set<Movie>().Add(entity); if (count % commitCount == 0) { db.SaveChanges(); if (recreatedContext) { db.Dispose(); db = new MovieTheaterRatingContext(); db.Configuration.AutoDetectChangesEnabled = false; } } return db; }
static void AddManyRecords() { List<Movie> reports = new List<Movie>(); Stopwatch sw = Stopwatch.StartNew(); var scopeOptions = new TransactionOptions(); scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; scopeOptions.Timeout = TimeSpan.MaxValue; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, scopeOptions)) { MovieTheaterRatingContext db = null; try { db = new MovieTheaterRatingContext(); db.Configuration.AutoDetectChangesEnabled = false; int count = 0; AddTestRecords(reports); foreach (var rep in reports) { ++count; db = AddToContext(db, rep, count, 100000, true); } db.SaveChanges(); } finally { if (db != null) db.Dispose(); } scope.Complete(); } sw.Stop(); Console.WriteLine("{0} milliseconds", sw.ElapsedMilliseconds); }