/
DataContext.Initializer.cs
59 lines (51 loc) · 2.14 KB
/
DataContext.Initializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Data.Entity;
using CustomExtensions.DataAnnotations;
namespace Ebuy.DataAccess
{
public partial class DataContext
{
public class Initializer : IDatabaseInitializer<DataContext>
{
private readonly IDatabaseInitializer<DataContext> _initializer;
public Initializer(IDatabaseInitializer<DataContext> initializer)
{
_initializer = initializer;
}
public void InitializeDatabase(DataContext context)
{
_initializer.InitializeDatabase(context);
Seed(context);
context.SaveChanges();
}
protected virtual void Seed(DataContext context)
{
// Apply the custom UniqueAttribute to set unique constraints
// on columns with the attribute defined
new UniqueConstraintApplier().ApplyUniqueConstraints(context);
context.Categories.Add(new Category("Collectibles"));
context.Categories.Add(
new Category("Electronics") {
SubCategories = new[] {
new Category("Cameras & Photography"),
new Category("Computers & Networking"),
new Category("TV, Audio, and Video"),
new Category("Video Games & Systems") {
SubCategories = new [] {
new Category("Video Game Systems")
}
},
}
});
context.Categories.Add(
new Category("Home & Outdoors")
{
SubCategories = new[] {
new Category("Home & Garden"),
new Category("Sporting Goods"),
new Category("Toys & Hobbies"),
}
});
}
}
}
}