using Microsoft.EntityFrameworkCore; using NLog; using System; using System.Linq; using System.Threading.Tasks; namespace MP.MONO.Data { public class DbAdmin : IDisposable { #region Private Fields private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Public Constructors public DbAdmin() { } #endregion Public Constructors #region Public Methods public static bool checkCreateUser(string username, string pwd) { bool answ = false; using (AdminContext adbCtx = new AdminContext()) { // ricerca utente... var numUser = adbCtx .UserList .Where(x => x.User == username) .ToList() .Count; if (numUser > 0) { answ = true; } if (!answ) { // creo utente string sqlCommand = "FLUSH PRIVILEGES;"; adbCtx.Database.ExecuteSqlRaw(sqlCommand); sqlCommand = $"CREATE USER '{username}'@'%' IDENTIFIED BY '{pwd}';"; adbCtx.Database.ExecuteSqlRaw(sqlCommand); sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'%';"; adbCtx.Database.ExecuteSqlRaw(sqlCommand); sqlCommand = "FLUSH PRIVILEGES;"; adbCtx.Database.ExecuteSqlRaw(sqlCommand); } } return answ; } public static async Task migrateDbIdentity() { bool answ = false; using (UserIdentityDbContext dbCtx = new UserIdentityDbContext()) { await dbCtx.Database.MigrateAsync(); answ = true; } return answ; } public static async Task migrateDbMain() { bool answ = false; using (MapoMonoContext dbCtx = new MapoMonoContext()) { await dbCtx.Database.MigrateAsync(); answ = true; } return answ; } public void Dispose() { } #endregion Public Methods } }