MafiaDataBase.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using MafiaTelegramBot.DataBase.Entity;
  2. using MafiaTelegramBot.Resources;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace MafiaTelegramBot.DataBase
  5. {
  6. public class MafiaDataBase : DbContext
  7. {
  8. private MafiaDataBase()
  9. {
  10. Database.EnsureCreated();
  11. }
  12. private static MafiaDataBase _instance;
  13. public static MafiaDataBase GetInstance()
  14. {
  15. if(_instance!=null) return _instance;
  16. _instance = new MafiaDataBase();
  17. return _instance;
  18. }
  19. public DbSet<UserEntity> Users { get; set; }
  20. public DbSet<StatisticsEntity> Statistics { get; set; }
  21. public DbSet<OpenedRolesEntity> OpenedRoles { get; set; }
  22. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  23. {
  24. optionsBuilder.UseMySQL(
  25. $"server={AppSettings.IpAddress};" +
  26. $"port={AppSettings.Port};" +
  27. $"database={AppSettings.Database};" +
  28. $"uid={AppSettings.Uid};" +
  29. $"pwd={AppSettings.Pwd}"
  30. );
  31. }
  32. protected override void OnModelCreating(ModelBuilder modelBuilder)
  33. {
  34. modelBuilder.Entity<StatisticsEntity>()
  35. .HasKey(s => new {s.UserId, s.Role});
  36. modelBuilder.Entity<OpenedRolesEntity>()
  37. .Property(r => r.Bodyguard).HasDefaultValue(0);
  38. modelBuilder.Entity<OpenedRolesEntity>()
  39. .Property(r => r.Dame).HasDefaultValue(0);
  40. modelBuilder.Entity<OpenedRolesEntity>()
  41. .Property(r => r.Detective).HasDefaultValue(0);
  42. modelBuilder.Entity<OpenedRolesEntity>()
  43. .Property(r => r.Elder).HasDefaultValue(0);
  44. modelBuilder.Entity<OpenedRolesEntity>()
  45. .Property(r => r.Fool).HasDefaultValue(0);
  46. modelBuilder.Entity<OpenedRolesEntity>()
  47. .Property(r => r.Hooker).HasDefaultValue(0);
  48. modelBuilder.Entity<OpenedRolesEntity>()
  49. .Property(r => r.Journalist).HasDefaultValue(0);
  50. modelBuilder.Entity<OpenedRolesEntity>()
  51. .Property(r => r.Lawyer).HasDefaultValue(0);
  52. modelBuilder.Entity<OpenedRolesEntity>()
  53. .Property(r => r.Necromancer).HasDefaultValue(0);
  54. modelBuilder.Entity<OpenedRolesEntity>()
  55. .Property(r => r.Parasite).HasDefaultValue(0);
  56. modelBuilder.Entity<OpenedRolesEntity>()
  57. .Property(r => r.Werewolf).HasDefaultValue(0);
  58. }
  59. }
  60. }