Chap4: Scaffolding a Store Manager (add database initialization strategy)

chap4_mvc5
jason.zhu 2021-06-22 16:00:20 +10:00
parent 9b8dad3957
commit 4021889c65
2 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using System;
using MvcMusicStore.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
@ -25,4 +26,27 @@ namespace MvcMusicStore.Data
public System.Data.Entity.DbSet<MvcMusicStore.Models.Genre> Genres { get; set; }
}
public class MusicStoreDbInitializer
: DropCreateDatabaseAlways<MusicStoreDB>
{
protected override void Seed(MusicStoreDB context)
{
context.Artists.Add(new Artist { Name = "Al Di Meola" });
context.Genres.Add(new Genre { Name = "Jazz" });
context.Albums.Add(new Album
{
Artist = new Artist { Name = "Rush" },
Genre = new Genre { Name = "Rock" },
Price = 9.99m,
Title = "Caravan"
});
base.Seed(context);
}
}
}

View File

@ -1,5 +1,7 @@
using System;
using MvcMusicStore.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
@ -8,10 +10,13 @@ using System.Web.Routing;
namespace MvcMusicStore
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer(new MusicStoreDbInitializer());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);