예제 #1
0
        private static async Task<IEnumerable<Fortune>> LoadRows(ApplicationDbContext dbContext)
        {
            var result = await dbContext.Fortune.ToListAsync();

            result.Add(new Fortune { Message = "Additional fortune added at request time." });
            result.Sort();

            return result;
        }
        private static async Task<World[]> LoadRows(int count, ApplicationDbContext dbContext)
        {
            var result = new World[count];

            for (int i = 0; i < count; i++)
            {
                var id = _random.Next(1, 10001);
                result[i] = await dbContext.World.FirstAsync(w => w.Id == id);
            }

            return result;
        }
예제 #3
0
        public bool Seed(ApplicationDbContext db)
        {
            if (!_seeded)
            {
                lock (_locker)
                {
                    if (!_seeded)
                    {
                        try
                        {
                            var count = db.World.Count();

                            if (count == 0)
                            {
                                var random = new Random();
                                for (int i = 0; i < 10000; i++)
                                {
                                    db.World.Add(new World { RandomNumber = random.Next(1, 10001) });
                                }
                                db.SaveChanges();
                                Console.WriteLine("Database successfully seeded!");
                            }
                            else
                            {
                                Console.WriteLine("Database already seeded!");
                            }

                            _seeded = true;
                            return true;
                        }
                        catch (Exception ex)
                        {
                            Console.Error.WriteLine("Error trying to seed the database. Have you run 'dnx ef database update'?");
                            Console.Error.WriteLine(ex);

                            return false;
                        }
                    }
                }
            }

            Console.WriteLine("Database already seeded!");
            return true;
        }
 public ApplicationDbSeeder(IRandom random, ApplicationDbContext dbContext)
 {
     _random = random;
     _dbContext = dbContext;
 }
예제 #5
0
        public bool Seed(ApplicationDbContext db)
        {
            if (!_seeded)
            {
                lock (_locker)
                {
                    if (!_seeded)
                    {
                        try
                        {
                            var world = db.World.Count();
                            var fortune = db.Fortune.Count();

                            if (world == 0 || fortune == 0)
                            {
                                if (world == 0)
                                {
                                    var random = new Random();
                                    for (int i = 0; i < 10000; i++)
                                    {
                                        db.World.Add(new World { RandomNumber = random.Next(1, 10001) });
                                    }
                                    db.SaveChanges();
                                }

                                if (fortune == 0)
                                {
                                    db.Fortune.Add(new Fortune { Message = "fortune: No such file or directory" });
                                    db.Fortune.Add(new Fortune { Message = "A computer scientist is someone who fixes things that aren't broken." });
                                    db.Fortune.Add(new Fortune { Message = "After enough decimal places, nobody gives a damn." });
                                    db.Fortune.Add(new Fortune { Message = "A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1" });
                                    db.Fortune.Add(new Fortune { Message = "A computer program does what you tell it to do, not what you want it to do." });
                                    db.Fortune.Add(new Fortune { Message = "Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen" });
                                    db.Fortune.Add(new Fortune { Message = "Any program that runs right is obsolete." });
                                    db.Fortune.Add(new Fortune { Message = "A list is only as strong as its weakest link. — Donald Knuth" });
                                    db.Fortune.Add(new Fortune { Message = "Feature: A bug with seniority." });
                                    db.Fortune.Add(new Fortune { Message = "Computers make very fast, very accurate mistakes." });
                                    db.Fortune.Add(new Fortune { Message = "<script>alert(\"This should not be displayed in a browser alert box.\");</script>" });
                                    db.Fortune.Add(new Fortune { Message = "フレームワークのベンチマーク" });

                                    db.SaveChanges();
                                }

                                Console.WriteLine("Database successfully seeded!");
                            }
                            else
                            {
                                Console.WriteLine("Database already seeded!");
                            }

                            _seeded = true;
                            return true;
                        }
                        catch (Exception ex)
                        {
                            Console.Error.WriteLine("Error trying to seed the database. Have you run 'dnx ef database update'?");
                            Console.Error.WriteLine(ex);

                            return false;
                        }
                    }
                }
            }

            Console.WriteLine("Database already seeded!");
            return true;
        }
예제 #6
0
        public void Configure(IApplicationBuilder app, ApplicationDbSeeder dbSeeder, ApplicationDbContext dbContext)
        {
            if (Scenarios.Plaintext)
            {
                app.UsePlainText();
            }

            if (Scenarios.Json)
            {
                app.UseJson();
            }

            // Single query endpoints
            if (Scenarios.DbSingleQueryRaw)
            {
                app.UseSingleQueryRaw();
            }

            if (Scenarios.DbSingleQueryDapper)
            {
                app.UseSingleQueryDapper();
            }

            if (Scenarios.DbSingleQueryEf)
            {
                app.UseSingleQueryEf();
            }

            // Multiple query endpoints
            if (Scenarios.DbMultiQueryRaw)
            {
                app.UseMultipleQueriesRaw();
            }

            if (Scenarios.DbMultiQueryDapper)
            {
                app.UseMultipleQueriesDapper();
            }

            if (Scenarios.DbMultiQueryEf)
            {
                app.UseMultipleQueriesEf();
            }

            // Multiple update endpoints
            if (Scenarios.DbMultiUpdateRaw)
            {
                app.UseMultipleUpdatesRaw();
            }

            if (Scenarios.DbMultiUpdateDapper)
            {
                app.UseMultipleUpdatesDapper();
            }

            if (Scenarios.DbMultiUpdateEf)
            {
                app.UseMultipleUpdatesEf();
            }

            // Fortunes endpoints
            if (Scenarios.DbFortunesRaw)
            {
                app.UseFortunesRaw();
            }

            if (Scenarios.DbFortunesDapper)
            {
                app.UseFortunesDapper();
            }

            if (Scenarios.DbFortunesEf)
            {
                app.UseFortunesEf();
            }

            if (Scenarios.Any("Db"))
            {
                dbContext.Database.EnsureCreated();

                if (!dbSeeder.Seed())
                {
                    Environment.Exit(1);
                }
            }

            if (Scenarios.Any("Mvc"))
            {
                app.UseMvc();
            }

            if (Scenarios.StaticFiles)
            {
                app.UseStaticFiles();
            }

            app.RunDebugInfoPage();
        }
예제 #7
0
 public EfDb(IRandom random, ApplicationDbContext dbContext)
 {
     _random = random;
     _dbContext = dbContext;
     _dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
 }