using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.ComponentModel.DataAnnotations;
namespace ProductRiaLib.Web.Models
{
public class ProductDbContext : DbContext
{
public ProductDbContext()
{
if (HttpContext.Current != null)
{
Database.SetInitializer<ProductDbContext>(new ProductDbContextInitializer());
}
}
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class ProductDbContextInitializer : DropCreateDatabaseIfModelChanges<ProductDbContext>
{
protected override void Seed(ProductDbContext context)
{
Category[] categories = new Category[]
{
new Category { CategoryID = 1, CategoryName = "Bath",
Products = new Product[] {
new Product { ProductID = 1, ProductName = "Bath Rug", UnitPrice = 24.5m},
new Product { ProductID = 2, ProductName = "Shower Curtain", UnitPrice = 30.99m},
new Product { ProductID = 3, ProductName = "Soap Dispenser", UnitPrice = 12.4m, OutOfStock = true},
new Product { ProductID = 4, ProductName = "Toilet Tissue", UnitPrice = 15},
}.ToList()},
new Category { CategoryID = 2, CategoryName = "Bedding",
Products = new Product[] {
new Product { ProductID = 5, ProductName = "Branket", UnitPrice = 60},
new Product { ProductID = 6, ProductName = "Mattress Protector", UnitPrice = 30.4m, OutOfStock = true },
new Product { ProductID = 7, ProductName = "Sheet Set", UnitPrice = 40.69m},
}.ToList()},
new Category { CategoryID = 3, CategoryName = "kitchen",
Products = new Product[] {
new Product { ProductID = 8, ProductName = "Baking Pan", UnitPrice = 10.99m},
new Product { ProductID = 9, ProductName = "Coffee Maker", UnitPrice = 49.39m},
new Product { ProductID = 10, ProductName = "Knife Set", UnitPrice = 70},
new Product { ProductID = 11, ProductName = "Pressure Cooker", UnitPrice = 90.5m, OutOfStock = true },
new Product { ProductID = 12, ProductName = "Water Pitcher", UnitPrice = 29.99m},
}.ToList()},
};
foreach (var category in categories)
{
context.Categories.Add(category);
}
context.SaveChanges();
}
}
}