120 lines
3.2 KiB
C#
120 lines
3.2 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 EgtBEAMWALL.DataLayer.Migrations;
|
|
//using EgtBEAMWALL.DataLayer.Controllers;
|
|
|
|
namespace EgtBEAMWALL.DataLayer
|
|
{
|
|
|
|
[DbConfigurationType(typeof(MySqlEFConfiguration))]
|
|
public class DatabaseContext : DbContext
|
|
{
|
|
/// <summary>
|
|
/// BTLParts management
|
|
/// </summary>
|
|
public DbSet<BTLPartModel> BTLPartList { get; set; }
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<ProdModel> ProdList { get; set; }
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<ProjModel> ProjList { get; set; }
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<RawPartModel> RawPartList { get; set; }
|
|
/// <summary>
|
|
/// Parts management
|
|
/// </summary>
|
|
public DbSet<PartModel> PartList { get; set; }
|
|
|
|
#if false
|
|
/// <summary>
|
|
/// Db data mode
|
|
/// </summary>
|
|
public static bool AdvDataModel { get; set; } = false;
|
|
#endif
|
|
|
|
//providerName="System.Data.EntityClient"
|
|
|
|
|
|
public static string CONNECTION_STRING = $"server={Constants.DATABASE_SERV};port=3306;database={Constants.DATABASE_NAME};uid={Constants.DATABASE_USER};pwd={Constants.DATABASE_PWD};sslmode=None";
|
|
|
|
public DatabaseContext() : base("DefaultConnection")
|
|
{
|
|
}
|
|
|
|
public static DatabaseContext Create()
|
|
{
|
|
return new DatabaseContext();
|
|
}
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
public static bool SetUpDbConnectionAndDbConfig()
|
|
{
|
|
try
|
|
{
|
|
String serviceName = getDbServiceName();
|
|
if (serviceName.Equals(""))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
ServiceController service = new ServiceController(serviceName);
|
|
try
|
|
{
|
|
TimeSpan timeout = TimeSpan.FromSeconds(Constants.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;
|
|
}
|
|
|
|
}
|
|
} |