Пример #1
0
        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);
        }
Пример #2
0
        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;
        }
        public void Setup()
        {
            connection = Effort.DbConnectionFactory.CreateTransient();
            using (var context = new MovieTheaterRatingContext(connection))
            {
                actorMovie1 = new ActorMovie
                {
                    ID = 1,
                    ActorID = 1,
                    CharacterInMovie = "Actor 1: First character",
                    MovieID = 1
                };

                actorMovie2 = new ActorMovie
                {
                    ID = 2,
                    ActorID = 1,
                    CharacterInMovie = "Actor 1: Second character",
                    MovieID = 1
                };

                actorMovie3 = new ActorMovie
                {
                    ID = 3,
                    ActorID = 2,
                    CharacterInMovie = "Actor 2: First character",
                    MovieID = 1
                };

                context.ActorMovies.Add(actorMovie1);
                context.ActorMovies.Add(actorMovie2);

                List<ActorMovie> moviePlayedByActor1 = new List<ActorMovie>();
                moviePlayedByActor1.Add(actorMovie1);
                moviePlayedByActor1.Add(actorMovie2);

                List<ActorMovie> moviePlayedByActor2 = new List<ActorMovie>();
                moviePlayedByActor2.Add(actorMovie3);

                actor1 = new Actor
                {
                    Id = 1,
                    FirstName = "First1",
                    LastName = "Last1",
                    ActorMovies = moviePlayedByActor1
                };

                actor2 = new Actor
                {
                    Id = 2,
                    FirstName ="First2",
                    LastName = "Last2",
                    ActorMovies = moviePlayedByActor2
                };

                context.Actors.Add(actor1);
                context.Actors.Add(actor2);

                List<ActorMovie> cast = new List<ActorMovie>();
                cast.Add(actorMovie1);
                cast.Add(actorMovie2);
                cast.Add(actorMovie3);
                movie = new Movie
                {
                    Id = 1,
                    Title = "Movie 1",
                    ActorMovies = cast
                };
                context.Movies.Add(movie);
                context.SaveChanges();
            }

            _context = new MovieTheaterRatingContext(connection);
            _context.Database.Initialize(true);
        }