Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using GTMPGameMode.GermanRealLife.Model;
  2. using MySql.Data.Entity;
  3. using MySql.Data.MySqlClient;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Data.Entity;
  8. using System.Data.Entity.Infrastructure;
  9. using System.Data.Entity.Migrations;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13.  
  14. namespace GTMPGameMode.GermanRealLife.Base.Database
  15. {
  16.  
  17.     [DbConfigurationType(typeof(MySqlEFConfiguration))]
  18.     public class DatabaseContext : DbContext
  19.     {
  20.         public DatabaseContext(string connectionString) : base(connectionString)
  21.         {
  22.             //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
  23.             //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
  24.             System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
  25.         }
  26.  
  27.         public DbSet<Account> Accounts { get; set; }
  28.     }
  29.  
  30.     public class ContextFactory : IDbContextFactory<DatabaseContext>
  31.     {
  32.         private static string ConnectionString = "Server=localhost;Database=germanreallife;Uid=root;Pwd=;";
  33.  
  34.         public static void SetConnectionParameters(string serverAddress, string username, string password, string database, uint port = 3306)
  35.         {
  36.             var connectionStringBuilder = new MySqlConnectionStringBuilder()
  37.             {
  38.                 Server = serverAddress,
  39.                 UserID = username,
  40.                 Password = password,
  41.                 Database = database,
  42.                 Port = port
  43.             };
  44.  
  45.             ConnectionString = connectionStringBuilder.ToString();
  46.         }
  47.  
  48.         private static DatabaseContext _instance;
  49.  
  50.         public static DatabaseContext Instance
  51.         {
  52.             get
  53.             {
  54.                 if (_instance != null) return _instance;
  55.                 return _instance = new ContextFactory().Create();
  56.             }
  57.             private set { }
  58.         }
  59.  
  60.         public DatabaseContext Create()
  61.         {
  62.             if (string.IsNullOrEmpty(ConnectionString)) throw new InvalidOperationException("Please set the connection parameters before trying to instantiate a database connection.");
  63.  
  64.             return new DatabaseContext(ConnectionString);
  65.         }
  66.     }
  67.  
  68.     internal sealed class MigrationConfiguration : DbMigrationsConfiguration<DatabaseContext>
  69.     {
  70.         public MigrationConfiguration()
  71.         {
  72.             AutomaticMigrationsEnabled = true;
  73.             AutomaticMigrationDataLossAllowed = true;
  74.  
  75.             SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement