Exemple #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CustomersDbContext(serviceProvider.GetRequiredService <DbContextOptions <CustomersDbContext> >()))
            {
                // If the Customers Data Store already has data then
                // just exit the Initialize method.
                if (context.Customers.Any())
                {
                    return;
                }

                // Initial values of the customers in our Data Store.
                context.Customers.AddRange
                (
                    new Customer {
                    Id = 1, firstName = "Steve", lastName = "Rogers", dateOfBirth = new DateTime(1910, 1, 1)
                },
                    new Customer {
                    Id = 2, firstName = "Tony", lastName = "Stark", dateOfBirth = new DateTime(1970, 2, 2)
                },
                    new Customer {
                    Id = 3, firstName = "Bruce", lastName = "Banner", dateOfBirth = new DateTime(1975, 3, 3)
                },
                    new Customer {
                    Id = 4, firstName = "Peter", lastName = "Parker", dateOfBirth = new DateTime(2002, 4, 4)
                }
                );

                context.SaveChanges();
            }
        }
Exemple #2
0
        public static void EnsureSeeded(this CustomersDbContext context)
        {
            //Ensure we have some status
            if (context.Customers.Any())
            {
                return;
            }

            // Initial values of the customers in our Data Store.
            context.Customers.AddRange
            (
                new Customer {
                Id = 1, firstName = "Alan", lastName = "Chen", dateOfBirth = new DateTime(1970, 2, 3)
            },
                new Customer {
                Id = 2, firstName = "Mikka", lastName = "Singh", dateOfBirth = new DateTime(1985, 4, 2)
            },
                new Customer {
                Id = 3, firstName = "Tom", lastName = "Hanks", dateOfBirth = new DateTime(1962, 3, 4)
            },
                new Customer {
                Id = 4, firstName = "Julia", lastName = "Roberts", dateOfBirth = new DateTime(1972, 4, 4)
            }
            );

            context.SaveChanges();
        }