-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShopContextDbInitializer.cs
76 lines (61 loc) · 2.94 KB
/
ShopContextDbInitializer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
using System.Data.Entity;
using Bogus;
using Shop.Entities.Concrete;
namespace Shop.DataAccess.Concrete.EntityFramework.Configuration.DatabaseInitializers
{
internal static class ShopContextDbInitializer
{
public class DropCreateAlways : DropCreateDatabaseAlways<ShopContext>
{
public override void InitializeDatabase(ShopContext context)
{
//context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, $"ALTER DATABASE [{context.Database.Connection.Database}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
base.InitializeDatabase(context);
}
protected override void Seed(ShopContext context)
{
InitDatabase(context);
}
}
public static void InitDatabase(ShopContext context)
{
const int minProductQuantityPerCategory = 2;
const int maxProductQuantityPerCategory = 5;
var faker = new Faker("tr");
// Add roles
var roles = new List<Role>
{
new Role { Name = "Admin" },
new Role { Name = "Manager" },
new Role { Name = "User" }
};
context.Roles.AddRange(roles);
context.SaveChanges();
// Add users
var users = new List<User>
{
new User { FirstName = faker.Name.FirstName(), LastName = faker.Name.LastName(), Password = "1234", Username = "admin", Roles = new List<Role> { roles[0] } },
new User { FirstName = faker.Name.FirstName(), LastName = faker.Name.LastName(), Password = "1234", Username = "manager", Roles = new List<Role> { roles[1], roles[2] } },
new User { FirstName = faker.Name.FirstName(), LastName = faker.Name.LastName(), Password = "1234", Username = "staff", Roles = new List<Role> { roles[2] } }
};
context.Users.AddRange(users);
context.SaveChanges();
// products
var productIds = 0;
var products = new Faker<Product>()
.RuleFor(x => x.Id, f => productIds++)
.RuleFor(x => x.UnitPrice, f => decimal.Parse(f.Commerce.Price(10, 500)))
.RuleFor(x => x.UnitsInStock, f => f.Random.Short(0, 100))
.RuleFor(x => x.Name, (f, p) => $"{f.Commerce.ProductName()}_{p.Id}");
// Add categories
var categoryIds = 0;
var categories = new Faker<Category>()
.RuleFor(x => x.Id, f => categoryIds++)
.RuleFor(x => x.Name, (f, c) => $"{f.Commerce.Department()}_{c.Id}")
.RuleFor(x => x.Products, f => products.Generate(f.Random.Number(minProductQuantityPerCategory, maxProductQuantityPerCategory)));
context.Categories.AddRange(categories.Generate(5));
context.SaveChanges();
}
}
}