示例#1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new MvcBookContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <MvcBookContext> >()))
            {
                // Look for any Books.
                if (context.Book.Any())
                {
                    return;   // DB has been seeded
                }

                context.Book.AddRange(
                    new Book

                {
                    Title       = "When Harry Met Sally",
                    ReleaseDate = DateTime.Parse("1989-2-12"),
                    Genre       = "Romantic Comedy",
                    Price       = 7.99M
                },

                    new Book
                {
                    Title       = "Ghostbusters ",
                    ReleaseDate = DateTime.Parse("1984-3-13"),
                    Genre       = "Comedy",
                    Price       = 8.99M
                },

                    new Book
                {
                    Title       = "Ghostbusters 2",
                    ReleaseDate = DateTime.Parse("1986-2-23"),
                    Genre       = "Comedy",
                    Price       = 9.99M
                },

                    new Book
                {
                    Title       = "Rio Bravo",
                    ReleaseDate = DateTime.Parse("1959-4-15"),
                    Genre       = "Western",
                    Price       = 3.99M
                }
                    );
                context.SaveChanges();
            }
        }
示例#2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new MvcBookContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <MvcBookContext> >()))
            {
                // Look for any books.
                if (context.Book.Any())
                {
                    // DB has been seeded
                }
                else
                {
                    context.Book.AddRange(
                        new Book
                    {
                        Title       = "Harry Potter and the Chamber of Secrets",
                        Author      = "J.K. Rowling",
                        PublishDate = DateTime.Parse("1998-2-07"),
                        Genre       = "Fantasy literature",
                        Rating      = "4.3",
                        Price       = 7.99M
                    },

                        new Book
                    {
                        Title       = "The Lost Symbol",
                        Author      = "Dan Brown",
                        PublishDate = DateTime.Parse("1984-3-13"),
                        Genre       = "Thriller",
                        Rating      = "3.7",
                        Price       = 8.99M
                    },

                        new Book
                    {
                        Title       = "The Lord of the Rings",
                        Author      = "J. R. R. Tolkien",
                        PublishDate = DateTime.Parse("2009-9-15"),
                        Genre       = "Adventure fiction",
                        Rating      = "4.4",
                        Price       = 9.99M
                    },

                        new Book
                    {
                        Title       = "Romeo and Juliet",
                        Author      = "William Shakespeare",
                        PublishDate = DateTime.Parse("1597-01-01"),
                        Genre       = "Play",
                        Rating      = "4.5",
                        Price       = 3.97M
                    }
                        );
                }

                if (context.Role.Any())
                {
                    // DB has been seeded
                }
                else
                {
                    context.Role.AddRange(
                        new Role

                    {
                        Id   = "6",
                        Name = "User"
                    }
                        );
                }


                context.SaveChanges();
            }
        }