Advertisement
Guest User

Untitled

a guest
May 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using GTMPGameMode.GermanRealLife.Server.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.  
  12. namespace MySQLLinqExample.resources.mysql.Server.Models
  13. {
  14.     [DbConfigurationType(typeof(MySqlEFConfiguration))]
  15.     public class DefaultDbContext : DbContext
  16.     {
  17.         public DefaultDbContext(string connectionString) : base(connectionString)
  18.         {
  19.             //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DefaultDbContext, MigrationConfiguration>(true, new MigrationConfiguration()));
  20.             Database.SetInitializer(new MigrateDatabaseToLatestVersion<DefaultDbContext, MigrationConfiguration>());
  21.         }
  22.  
  23.         public override int SaveChanges()
  24.         {
  25.             var entities = (from entry in ChangeTracker.Entries()
  26.                             where entry.State == EntityState.Modified || entry.State == EntityState.Added
  27.                             select entry.Entity);
  28.  
  29.             var validationResults = new List<ValidationResult>();
  30.             foreach (var entity in entities)
  31.             {
  32.                 if (!Validator.TryValidateObject(entity, new ValidationContext(entity), validationResults))
  33.                 {
  34.                     throw new ValidationException();
  35.                 }
  36.             }
  37.             return base.SaveChanges();
  38.         }
  39.  
  40.         public DbSet<account> Accounts { get; set; }
  41.     }
  42.  
  43.     public class ContextFactory : IDbContextFactory<DefaultDbContext>
  44.     {
  45.         private static string ConnectionString;
  46.  
  47.         public static void SetConnectionParameters(string serverAddress, string username, string password, string database, uint port = 3306)
  48.         {
  49.             var connectionStringBuilder = new MySqlConnectionStringBuilder()
  50.             {
  51.                 Server = serverAddress,
  52.                 UserID = username,
  53.                 Password = password,
  54.                 Database = database,
  55.                 Port = port
  56.             };
  57.  
  58.             ConnectionString = connectionStringBuilder.ToString();
  59.         }
  60.  
  61.         private static DefaultDbContext _instance;
  62.  
  63.         public static DefaultDbContext Instance
  64.         {
  65.             get
  66.             {
  67.                 if (_instance != null) return _instance;
  68.                 return _instance = new ContextFactory().Create();
  69.             }
  70.             private set { }
  71.         }
  72.  
  73.         public DefaultDbContext Create()
  74.         {
  75.             if (string.IsNullOrEmpty(ConnectionString)) throw new InvalidOperationException("Please set the connection parameters before trying to instantiate a database connection.");
  76.  
  77.             return new DefaultDbContext(ConnectionString);
  78.         }
  79.     }
  80.  
  81.     internal sealed class MigrationConfiguration : DbMigrationsConfiguration<DefaultDbContext>
  82.     {
  83.         public MigrationConfiguration()
  84.         {
  85.            
  86.             AutomaticMigrationsEnabled = true;
  87.             AutomaticMigrationDataLossAllowed = true;
  88.             SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement