public static void Initialize(IServiceProvider serviceProvider) { using (var context = new BangazonContext(serviceProvider.GetRequiredService <DbContextOptions <BangazonContext> >())) { if (context.Customer.Any()) { return; //db is already seeded } var customer = new Customer[] { new Customer { FirstName = "Gucci", LastName = "Mane" }, new Customer { FirstName = "Riff", LastName = "Raff" }, new Customer { FirstName = "Wacka Flocka", LastName = "Flame" } }; foreach (Customer i in customer) { context.Customer.Add(i); } context.SaveChanges(); var productsType = new ProductType[] { new ProductType { ProductTypeName = "Sports" }, new ProductType { ProductTypeName = "Toys" } }; foreach (ProductType i in productsType) { context.ProductType.Add(i); } context.SaveChanges(); var products = new Product[] { new Product { ProductTypeId = productsType.Single(s => s.ProductTypeName == "Sports").ProductTypeId, Price = 50.00, Title = "Baseball Glove", Description = "This glove will help you catch baseballs.", CustomerId = customer.Single(s => s.FirstName == "Wacka Flocka").CustomerId }, new Product { ProductTypeId = productsType.Single(s => s.ProductTypeName == "Sports").ProductTypeId, Price = 30.00, Title = "Basketball", Description = "Learn to dunk!", CustomerId = customer.Single(s => s.FirstName == "Riff").CustomerId }, new Product { ProductTypeId = productsType.Single(s => s.ProductTypeName == "Toys").ProductTypeId, Price = 10.00, Title = "Teddy Bear", Description = "Get it for the kids.", CustomerId = customer.Single(s => s.FirstName == "Wacka Flocka").CustomerId }, new Product { ProductTypeId = productsType.Single(s => s.ProductTypeName == "Toys").ProductTypeId, Price = 5.00, Title = "Coloring Book", Description = "Stay in the lines. Or don't. Its up to you.", CustomerId = customer.Single(s => s.FirstName == "Gucci").CustomerId } }; foreach (Product i in products) { context.Product.Add(i); } context.SaveChanges(); var paymentTypes = new PaymentType[] { new PaymentType { CustomerId = customer.Single(s => s.FirstName == "Gucci").CustomerId, AccountNumber = 2, PaymentTypeName = "Cash", }, new PaymentType { CustomerId = customer.Single(s => s.FirstName == "Riff").CustomerId, AccountNumber = 3, PaymentTypeName = "Visa", }, new PaymentType { CustomerId = customer.Single(s => s.FirstName == "Wacka Flocka").CustomerId, AccountNumber = 4, PaymentTypeName = "Mastercard", } }; foreach (PaymentType i in paymentTypes) { context.PaymentType.Add(i); } context.SaveChanges(); var orders = new Order[] { new Order { CustomerId = customer.Single(s => s.FirstName == "Wacka Flocka").CustomerId, // PaymentTypeId = paymentTypes.Single(s => s.PaymentTypeName == "Cash").PaymentTypeId, }, new Order { CustomerId = customer.Single(s => s.FirstName == "Riff").CustomerId, // PaymentTypeId = paymentTypes.Single(s => s.PaymentTypeName == "Visa").PaymentTypeId, }, new Order { CustomerId = customer.Single(s => s.FirstName == "Gucci").CustomerId, // PaymentTypeId = paymentTypes.Single(s => s.PaymentTypeName == "Mastercard").PaymentTypeId, } }; foreach (Order i in orders) { context.Order.Add(i); } context.SaveChanges(); var orderProduct = new OrderProduct[] { new OrderProduct { OrderId = 1, ProductId = products.Single(s => s.Title == "Baseball Glove").ProductId }, new OrderProduct { OrderId = 1, ProductId = products.Single(s => s.Title == "Teddy Bear").ProductId }, new OrderProduct { OrderId = 2, ProductId = products.Single(s => s.Title == "Basketball").ProductId }, new OrderProduct { OrderId = 3, ProductId = products.Single(s => s.Title == "Coloring Book").ProductId } }; foreach (OrderProduct i in orderProduct) { context.OrderProduct.Add(i); } context.SaveChanges(); var departments = new Department[] { new Department { DeptName = "Sporting Goods", ExpenseBudget = 1500 }, new Department { DeptName = "Toy Department", ExpenseBudget = 1250 } }; foreach (Department i in departments) { context.Department.Add(i); } context.SaveChanges(); var employees = new Employee[] { new Employee { EmployeeName = "A$AP Ferg", EmployeePhone = "212-555-1212", DepartmentId = departments.Single(s => s.DeptName == "Sporting Goods").DepartmentId, IsSupervisor = false, }, new Employee { EmployeeName = "A$AP Rocky", EmployeePhone = "212-555-1213", DepartmentId = departments.Single(s => s.DeptName == "Sporting Goods").DepartmentId, IsSupervisor = true, }, new Employee { EmployeeName = "A$AP Nast", EmployeePhone = "212-555-1214", DepartmentId = departments.Single(s => s.DeptName == "Toy Department").DepartmentId, IsSupervisor = false, } }; foreach (Employee i in employees) { context.Employee.Add(i); } context.SaveChanges(); var computers = new Computer[] { new Computer { ModelNumber = 123 }, new Computer { ModelNumber = 456 }, new Computer { ModelNumber = 789 } }; foreach (Computer i in computers) { context.Computer.Add(i); } context.SaveChanges(); var employeeComputers = new EmployeeComputer[] { new EmployeeComputer { ComputerId = computers.Single(s => s.ModelNumber == 123).ComputerId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Nast").EmployeeId }, new EmployeeComputer { ComputerId = computers.Single(s => s.ModelNumber == 456).ComputerId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Ferg").EmployeeId }, new EmployeeComputer { ComputerId = computers.Single(s => s.ModelNumber == 123).ComputerId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Rocky").EmployeeId } }; foreach (EmployeeComputer i in employeeComputers) { context.EmployeeComputer.Add(i); } context.SaveChanges(); var training = new Training[] { new Training { StartDate = new DateTime(2017, 08, 01), EndDate = new DateTime(2017, 08, 07), Name = "Computer Training", MaxAttendees = 25 }, new Training { StartDate = new DateTime(2017, 09, 01), EndDate = new DateTime(2017, 09, 07), Name = "Up Selling", MaxAttendees = 40 }, new Training { StartDate = new DateTime(2017, 09, 10), EndDate = new DateTime(2017, 09, 12), Name = "Sensitivity Traning", MaxAttendees = 6 } }; foreach (Training i in training) { context.Training.Add(i); } context.SaveChanges(); var employeeTrainings = new EmployeeTraining[] { new EmployeeTraining { TrainingId = training.Single(s => s.Name == "Computer Training").TrainingId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Nast").EmployeeId }, new EmployeeTraining { TrainingId = training.Single(s => s.Name == "Up Selling").TrainingId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Ferg").EmployeeId }, new EmployeeTraining { TrainingId = training.Single(s => s.Name == "Sensitivity Traning").TrainingId, EmployeeId = employees.Single(s => s.EmployeeName == "A$AP Rocky").EmployeeId } }; foreach (EmployeeTraining i in employeeTrainings) { context.EmployeeTraining.Add(i); } context.SaveChanges(); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new BangazonContext(serviceProvider.GetRequiredService <DbContextOptions <BangazonContext> >())) { if (context.Customer.Any()) { return; } //seeding CUSTOMERS var customers = new Customer[] { new Customer { FirstName = "Jelly", LastName = "Otter", Active = 0 }, new Customer { FirstName = "Nigel", LastName = "Thornberry", Active = 1 }, new Customer { FirstName = "Frank", LastName = "Underwood", Active = 1 }, }; foreach (Customer i in customers) { context.Customer.Add(i); } context.SaveChanges(); //seeding COMPUTERS var computers = new Computer[] { new Computer { }, new Computer { }, new Computer { } }; foreach (Computer i in computers) { context.Computer.Add(i); } context.SaveChanges(); //seeding DEPARTMENTS var departments = new Department[] { new Department { Name = "Marketing", Budget = 123456 }, new Department { Name = "Accounting", Budget = 234567 } }; foreach (Department i in departments) { context.Department.Add(i); } context.SaveChanges(); //seeding EMPLOYEES var employees = new Employee[] { new Employee { Name = "Jon Snow" }, new Employee { Name = "Stephen Spielburg" }, new Employee { Name = "Megan Berry" } }; foreach (Employee i in employees) { context.Employee.Add(i); } context.SaveChanges(); //seeding PAYMENT TYPES var paymentTypes = new PaymentType[] { new PaymentType { Type = "Visa", AccountNumber = "12345667890", CustomerId = customers.Single(firstname => firstname.FirstName == "Jelly").CustomerId, }, new PaymentType { Type = "MasterCard", AccountNumber = "2345678901", CustomerId = customers.Single(firstname => firstname.FirstName == "Nigel").CustomerId, }, new PaymentType { Type = "AmericanExpress", AccountNumber = "3456789012", CustomerId = customers.Single(firstname => firstname.FirstName == "Frank").CustomerId, } }; foreach (PaymentType i in paymentTypes) { context.PaymentType.Add(i); } context.SaveChanges(); //seeding PRODUCT TYPES var productTypes = new ProductType[] { new ProductType { Type = "Household" }, new ProductType { Type = "Appliance" }, new ProductType { Type = "Automotive" } }; foreach (ProductType i in productTypes) { context.ProductType.Add(i); } context.SaveChanges(); //seeding PRODUCTS var products = new Product[] { new Product { ProductTypeId = productTypes.Single(s => s.Type == "Household").ProductTypeId, SellerId = customers.Single(s => s.FirstName == "Jelly").CustomerId, Name = "Lamp", Description = "Lights stuff up", Price = 13 }, new Product { ProductTypeId = productTypes.Single(s => s.Type == "Appliance").ProductTypeId, SellerId = customers.Single(s => s.FirstName == "Nigel").CustomerId, Name = "Blender", Description = "Mixes stuff", Price = 13 }, new Product { ProductTypeId = productTypes.Single(s => s.Type == "Automotive").ProductTypeId, SellerId = customers.Single(s => s.FirstName == "Frank").CustomerId, Name = "Crowbar", Description = "Prys at stuff", Price = 13 } }; foreach (Product i in products) { context.Product.Add(i); } context.SaveChanges(); //seeding TRAINING PROGRAMS var trainingPrograms = new TrainingProgram[] { new TrainingProgram { Name = "Doing Your Best", MaxAttendees = 12, StartDate = new DateTime(2018, 8, 28, 2, 3, 0) }, new TrainingProgram { Name = "Excelling at Excel", MaxAttendees = 21, StartDate = new DateTime(2017, 8, 28, 2, 3, 0) }, new TrainingProgram { Name = "Bring Your Things", MaxAttendees = 3, StartDate = new DateTime(2016, 7, 28, 2, 3, 0) }, new TrainingProgram { Name = "Do Life Better", MaxAttendees = 45, StartDate = new DateTime(2015, 7, 28, 2, 3, 0) } }; foreach (TrainingProgram i in trainingPrograms) { context.TrainingProgram.Add(i); } context.SaveChanges(); //seeding ORDERS, scrambling order of customers so no initial sellerId will equal a buyerId var orders = new Order[] { new Order { CustomerId = customers.Single(f => f.FirstName == "Nigel").CustomerId, PaymentTypeId = paymentTypes.Single(t => t.Type == "Visa").PaymentTypeId }, new Order { CustomerId = customers.Single(f => f.FirstName == "Frank").CustomerId, PaymentTypeId = paymentTypes.Single(t => t.Type == "MasterCard").PaymentTypeId }, new Order { CustomerId = customers.Single(f => f.FirstName == "Jelly").CustomerId, PaymentTypeId = paymentTypes.Single(t => t.Type == "AmericanExpress").PaymentTypeId } }; foreach (Order i in orders) { context.Order.Add(i); } context.SaveChanges(); //seeding ORDER-PRODUCTS, matching up with the initialized orders and products by id var orderProducts = new OrderProduct[] { new OrderProduct { OrderId = orders.Single(o => o.OrderId == 1).OrderId, ProductId = products.Single(o => o.ProductId == 1).ProductId }, new OrderProduct { OrderId = orders.Single(o => o.OrderId == 2).OrderId, ProductId = products.Single(o => o.ProductId == 2).ProductId }, new OrderProduct { OrderId = orders.Single(o => o.OrderId == 3).OrderId, ProductId = products.Single(o => o.ProductId == 3).ProductId } }; foreach (OrderProduct i in orderProducts) { context.OrderProduct.Add(i); } context.SaveChanges(); //seeding COMPUTER-EMPLOYEES var computerEmployees = new ComputerEmployee[] { new ComputerEmployee { EmployeeId = employees.Single(e => e.Name == "Jon Snow").EmployeeId, ComputerId = computers.Single(c => c.ComputerId == 1).ComputerId, InDate = DateTime.Now }, new ComputerEmployee { EmployeeId = employees.Single(e => e.Name == "Stephen Spielburg").EmployeeId, ComputerId = computers.Single(c => c.ComputerId == 2).ComputerId, InDate = DateTime.Now }, new ComputerEmployee { EmployeeId = employees.Single(e => e.Name == "Megan Berry").EmployeeId, ComputerId = computers.Single(c => c.ComputerId == 3).ComputerId, InDate = DateTime.Now } }; foreach (ComputerEmployee i in computerEmployees) { context.ComputerEmployee.Add(i); } context.SaveChanges(); } }