Advertisement
Guest User

Database class

a guest
Jun 12th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using FluentNHibernate.Cfg;
  2. using FluentNHibernate.Cfg.Db;
  3. using NHibernate;
  4. using NHibernate.Cfg;
  5. using NHibernate.Tool.hbm2ddl;
  6.  
  7. namespace RockStars6
  8. {
  9.     public static class Database
  10.     {
  11.         /// <summary>
  12.         /// Gets a reference to the logger.
  13.         /// </summary>
  14.         //private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  15.  
  16.         public static void init()
  17.         {
  18.             // Need nHibernate init code here.
  19.             using (ISession database = OpenSession())
  20.             {
  21.                
  22.             }
  23.         }
  24.  
  25.         private static ISessionFactory _sessionFactory;
  26.  
  27.         public static ISessionFactory Factory
  28.         {
  29.             get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); }
  30.         }
  31.  
  32.         public static ISession OpenSession()
  33.         {
  34.             return Factory.OpenSession();
  35.         }
  36.  
  37.         private static ISessionFactory CreateSessionFactory()
  38.         {
  39.             return Fluently.Configure()
  40.                 .Database(
  41.                     MySQLConfiguration.Standard
  42.                     .ConnectionString("Server=127.0.0.1;Port=3306;Database=rockstars6dev;User ID=root;")
  43.                 )
  44.                 .Mappings(m =>
  45.                     m.FluentMappings.AddFromAssemblyOf<GameMode>())
  46.                 .ExposeConfiguration(TreatConfiguration)
  47.                 .BuildSessionFactory();
  48.         }
  49.            
  50.         private static void TreatConfiguration(Configuration configuration)
  51.         {
  52.             var update = new SchemaUpdate(configuration);
  53.             update.Execute(false, true);
  54.             //configuration.SetInterceptor(new SqlStatementInterceptor());
  55.  
  56.             // Expirimental: Create DB.
  57.             //new SchemaExport(configuration).Create(true, true);
  58.             var validateDatabase = new SchemaExport(configuration);
  59.             validateDatabase.SetOutputFile("GeneratedSQL.sql");
  60.             //validateDatabase.Execute(false, true, false);
  61.             // WARNING: Uncommenting the above line will WIPE THE ENTIRE DB!!
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement