Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTMPGameMode.GermanRealLife.Model;
- using MySql.Data.Entity;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Data.Entity.Migrations;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GTMPGameMode.GermanRealLife.Base.Database
- {
- [DbConfigurationType(typeof(MySqlEFConfiguration))]
- public class DatabaseContext : DbContext
- {
- public DatabaseContext(string connectionString) : base(connectionString)
- {
- //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
- //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
- System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, MigrationConfiguration>());
- }
- public DbSet<Account> Accounts { get; set; }
- }
- public class ContextFactory : IDbContextFactory<DatabaseContext>
- {
- private static string ConnectionString = "Server=localhost;Database=germanreallife;Uid=root;Pwd=;";
- public static void SetConnectionParameters(string serverAddress, string username, string password, string database, uint port = 3306)
- {
- var connectionStringBuilder = new MySqlConnectionStringBuilder()
- {
- Server = serverAddress,
- UserID = username,
- Password = password,
- Database = database,
- Port = port
- };
- ConnectionString = connectionStringBuilder.ToString();
- }
- private static DatabaseContext _instance;
- public static DatabaseContext Instance
- {
- get
- {
- if (_instance != null) return _instance;
- return _instance = new ContextFactory().Create();
- }
- private set { }
- }
- public DatabaseContext Create()
- {
- if (string.IsNullOrEmpty(ConnectionString)) throw new InvalidOperationException("Please set the connection parameters before trying to instantiate a database connection.");
- return new DatabaseContext(ConnectionString);
- }
- }
- internal sealed class MigrationConfiguration : DbMigrationsConfiguration<DatabaseContext>
- {
- public MigrationConfiguration()
- {
- AutomaticMigrationsEnabled = true;
- AutomaticMigrationDataLossAllowed = true;
- SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement