using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Migrations; using MySql.Data.Entity; using System.Linq; using System.Text; using EgtBEAMWALL.DataLayer.DatabaseModels; using EgtBEAMWALL.DataLayer.Migrations; using System.ServiceProcess; using System.IO; using System.Data.Entity.Infrastructure; namespace EgtBEAMWALL.DataLayer { //[DbConfigurationType(typeof(MySqlEFConfiguration))] public class aMySqlConfiguration : MySqlEFConfiguration { #region Public Constructors public aMySqlConfiguration() : base() { var path = Path.GetDirectoryName(this.GetType().Assembly.Location); SetModelStore(new DefaultDbModelStore(path)); } #endregion Public Constructors } [DbConfigurationType(typeof(MySqlEFConfiguration))] //[DbConfigurationType(typeof(aMySqlConfiguration))] public class DatabaseContext : DbContext { #region Public Constructors [Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")] public DatabaseContext() : base("DefaultConnection") { } public DatabaseContext(string connectionString) : base(connectionString) { Database.CreateIfNotExists(); //Database.SetInitializer(new MigrateDatabaseToLatestVersion()); Database.Initialize(false); } #endregion Public Constructors #region Public Properties /// /// BTLParts management /// public DbSet BTLPartList { get; set; } /// /// Machine Log management /// public DbSet LogMachineList { get; set; } /// /// Support Log management /// public DbSet LogSupportList { get; set; } /// /// Parts management /// public DbSet MachGroupList { get; set; } /// /// Parts management /// public DbSet PartList { get; set; } /// /// Parts management /// public DbSet ProdList { get; set; } /// /// Parts management /// public DbSet ProjList { get; set; } /// /// StatusMap management /// public DbSet StatusMapList { get; set; } #endregion Public Properties #if false /// /// Db data mode /// public static bool AdvDataModel { get; set; } = false; #endif #region Private Methods private static string getDbServiceName() { ServiceController[] services = ServiceController.GetServices(); var service = services.FirstOrDefault(s => s.ServiceName == "MariaDB"); if (service != null) return service.DisplayName; service = services.FirstOrDefault(s => s.ServiceName == "MySQL"); if (service != null) return service.DisplayName; return ""; } #endregion Private Methods #region Public Methods public static DatabaseContext Create() { return new DatabaseContext(DbConfig.CONNECTION_STRING); } public static bool SetUpDbConnectionAndDbConfig() { try { String serviceName = getDbServiceName(); if (serviceName.Equals("")) { return false; } ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromSeconds(DbConfig.DATABASE_PROCESS_TIMEOUT); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception ex) { return false; } System.Data.Entity.Database.SetInitializer(null); var migrator = new DbMigrator(new Configuration()); if (migrator.GetPendingMigrations().Any()) { // Run migrations and seed. migrator.Update(); } } catch (Exception ex) { return false; } return true; } #endregion Public Methods } }