forked from pruet/DNWS
-
Notifications
You must be signed in to change notification settings - Fork 46
/
WerewolfContext.cs
57 lines (54 loc) · 2.07 KB
/
WerewolfContext.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
using System;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace DNWS.Werewolf
{
/// <summary>
///
/// </summary>
public partial class WerewolfContext : DbContext
{
public DbSet<Role> Roles { get; set; }
public DbSet<Game> Games { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Action> Actions { get; set; }
public DbSet<ActionRole> ActionRoles { get; set; }
public DbSet<ChatMessage> ChatMessages { get; set; }
private static bool _created = false;
public string myguid;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ActionRole>()
.HasKey(ar => new {ar.ActionId, ar.RoleId});
modelBuilder.Entity<ActionRole>()
.HasOne(ar => ar.Action)
.WithMany(r => r.ActionRoles)
.HasForeignKey(ar => ar.ActionId);
modelBuilder.Entity<ActionRole>()
.HasOne(ar => ar.Role)
.WithMany(a => a.ActionRoles)
.HasForeignKey(ar => ar.RoleId);
modelBuilder.Entity<Player>()
.HasOne(p => p.Game)
.WithMany(g => g.Players)
.HasForeignKey(p => p.GameId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// uncomment next line to enable sqlite storage (you need to comment out MySQL line as well)
optionsBuilder.UseSqlite("Data Source=werewolf.db");
//optionsBuilder.UseMySQL("server=localhost;database=werewolf;user=werewolf;password=werewolf;SslMode=none");
optionsBuilder.EnableSensitiveDataLogging();
}
public WerewolfContext()
{
myguid = Guid.NewGuid().ToString();
if (!_created) {
_created = true;
//Database.EnsureDeleted();
Database.EnsureCreated();
}
}
}
}