Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder <StudentContext>()
                          .UseInMemoryDatabase(databaseName: "MockStudents")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new StudentContext(options))
            {
                var studentRepo    = new EfRepository <Student>(context);
                var enrollmentRepo = new EfRepository <Enrollment>(context);
                var serviceRepo    = new EfRepository <Service>(context);

                StudentContextSeed.Seed(context, null);

                Console.WriteLine($"Students: {studentRepo.ListAllAsync().Result.Count}");
                Console.WriteLine($"Enrollments: {enrollmentRepo.ListAllAsync().Result.Count}");
                Console.WriteLine($"Services: {serviceRepo.ListAllAsync().Result.Count}");
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var dbContext = services.GetRequiredService <StudentContext>();
                    StudentContextSeed.Seed(dbContext, loggerFactory);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }