public static void Initialize(IServiceProvider serviceProvider) { using (var context = new MvcMovieContext (serviceProvider.GetRequiredService <DbContextOptions <MvcMovieContext> >())) { foreach (var movie in context.Movie) { context.Remove(movie); } context.SaveChanges(); //is there a existing in my db if (context.Movie.Any()) { return; //exit because db is seeded } context.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-1-11"), Genre = "Romantic Comedy", Price = 7.99M, Rating = "R" }, new Movie { Title = "GhostBusters", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M, Rating = "PG" }, new Movie { Title = "GhostBusters 2", ReleaseDate = DateTime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M, Rating = "PG" }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M, Rating = "NR" } ); context.SaveChanges(); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new MvcMovieContext(serviceProvider.GetRequiredService <DbContextOptions <MvcMovieContext> >())) { foreach (var movie in context.Movie) { context.Remove(movie); } context.SaveChanges(); if (context.Movie.Any()) { return; //exit because db is seeded } { context.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-1-11"), Genre = "Romantic Comedy", Price = 7.99M, Rating = "R" }, new Movie { Title = "Kal ho na ho", ReleaseDate = DateTime.Parse("2008-1-12"), Genre = "Drama", Price = 8.88M, Rating = "PG" }, new Movie { Title = "Dostana", ReleaseDate = DateTime.Parse("2009-2-10"), Genre = "Comedy", Price = 5.99M, Rating = "PG" } ); context.SaveChanges(); } } }