Browse Source

Added creation database tables if not exist

Veloe 4 years ago
parent
commit
d67e4b53a5

+ 5 - 4
MafiaTelegramBot/DataBase/Entity/StatisticsEntity.cs

@@ -1,4 +1,5 @@
 using System;
+using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.Threading.Tasks;
 using MafiaTelegramBot.DataBase.EntityDao;
@@ -9,15 +10,15 @@ namespace MafiaTelegramBot.DataBase.Entity
     [Table("statistics")]
     public class StatisticsEntity
     {
-        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("id")]
+        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("id") , MaxLength(127)]
         public long UserId { get; init; }
-        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("role")]
+        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("role") , MaxLength(127)]
         public string Role { get; init; }
         
         [NotMapped]
         private int _games = 0;
         
-        [Column("games")]
+        [Column("games") , MaxLength(127)]
         public int Games
         {
             get=> _games;
@@ -32,7 +33,7 @@ namespace MafiaTelegramBot.DataBase.Entity
         [NotMapped]
         private int _wins = 0;
         
-        [Column("wins")]
+        [Column("wins") , MaxLength(127)]
         public int Wins
         {
             get=> _wins;

+ 4 - 4
MafiaTelegramBot/DataBase/Entity/UserEntity.cs

@@ -10,11 +10,11 @@ namespace MafiaTelegramBot.DataBase.Entity
     {
         [Key]
         [DatabaseGenerated(DatabaseGeneratedOption.None)]
-        [Column("id")] public long Id { get; init; }
-        [Column("chat_id")] public long ChatId { get; init; }
+        [Column("id"), MaxLength(127)] public long Id { get; init; }
+        [Column("chat_id") , MaxLength(127)] public long ChatId { get; init; }
         
         [NotMapped] private string _username = "\\[NoUsername\\]";
-        [Column("username")] public string Username
+        [Column("username") , MaxLength(127)] public string Username
         {
             get => _username;
             set
@@ -25,7 +25,7 @@ namespace MafiaTelegramBot.DataBase.Entity
             }
         }
 
-        [Column("nickname")] public string NickName { get; set; } = "\\[NoNickname\\]";
+        [Column("nickname") , MaxLength(127)] public string NickName { get; set; } = "\\[NoNickname\\]";
 
         public async Task UpdateNickName(string newName)
         {

+ 7 - 2
MafiaTelegramBot/DataBase/MafiaDataBase.cs

@@ -1,21 +1,26 @@
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.Resources;
 using Microsoft.EntityFrameworkCore;
+using Telegram.Bot.Types;
 
 namespace MafiaTelegramBot.DataBase
 {
     public class MafiaDataBase : DbContext
     {
-        private MafiaDataBase() { }
+        private MafiaDataBase()
+        {
+            Database.EnsureCreated();
+        }
         private static MafiaDataBase _instance;
 
         public static MafiaDataBase GetInstance()
         {
             if(_instance!=null) return _instance;
             _instance = new MafiaDataBase();
+            
             return _instance;
         }
-
+        
         public DbSet<UserEntity> Users { get; set; }
         public DbSet<StatisticsEntity> Statistics { get; set; }