166 lines
4.7 KiB
C#
166 lines
4.7 KiB
C#
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<StratonLocalizerDatabase, Migrations.Configuration>());
|
|
Database.Initialize(false);
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// BTLParts management
|
|
/// </summary>
|
|
public DbSet<BTLPartModel> BTLPartList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Machine Log management
|
|
/// </summary>
|
|
public DbSet<LogMachineModel> LogMachineList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Support Log management
|
|
/// </summary>
|
|
public DbSet<LogSupportModel> LogSupportList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<MachGroupModel> MachGroupList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<PartModel> PartList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<ProdModel> ProdList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<ProjModel> ProjList { get; set; }
|
|
|
|
/// <summary>
|
|
/// StatusMap management
|
|
/// </summary>
|
|
public DbSet<StatusMapModel> StatusMapList { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Db data mode
|
|
/// </summary>
|
|
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<DatabaseContext>(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
|
|
}
|
|
} |