-
Notifications
You must be signed in to change notification settings - Fork 3
/
AccountDb.cs
56 lines (47 loc) · 1.75 KB
/
AccountDb.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
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Reflection;
namespace K2svc.Db
{
public class AccountDb
: DbContext // https://docs.microsoft.com/ko-kr/ef/core/miscellaneous/configuring-dbcontext
{
public DbSet<Account> Accounts { get; set; }
public AccountDb(ServiceConfiguration _config)
{
config = _config;
Database.EnsureCreated();
#if DEBUG
// TEST environment
if (Accounts.Count() == 0)
{
Accounts.AddRange(new Account[] {
new Account { AccountName = "k1", Password = "k1k1" },
new Account { AccountName = "k2", Password = "k2k2" },
new Account { AccountName = "k3", Password = "k3k3" },
new Account { AccountName = "k4", Password = "k4k4" },
});
SaveChanges();
}
#endif
}
#region DbContext override
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
// database provider 선택 ; 하드코딩없이 간편하게
builder.UseSqlite($"filename={config.DatabaseFileName}", options =>
{
options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
});
base.OnConfiguring(builder);
}
protected override void OnModelCreating(ModelBuilder builder)
{
// TODO: 하드코딩하지 않고 table 을 늘리거나 변경할 수 있도록(Attribute 이용?)
Account.OnModelCreating(builder.Entity<Account>());
base.OnModelCreating(builder);
}
#endregion
private readonly ServiceConfiguration config;
}
}