Example #1
0
        static NHibernateConfigurator()
        {
            var sqlitePersistenceConfigurerProvider = new SqlitePersistenceConfigurerProvider();

#if DEBUG
            FluentConfigurationSerializer.IsEnabled = false;
#endif
            var configurationProvider = new FluentConfigurationProvider(sqlitePersistenceConfigurerProvider, new AssemblyLocator());

            _configuration = configurationProvider.Configure().GetEnumerator().Current;

            _configuration = _configuration.DataBaseIntegration(db => db.ConnectionProvider <TestConnectionProvider> ());
            _configuration.SetProperty(Environment.CurrentSessionContextClass, "thread_static");

            var props = _configuration.Properties;
            if (props.ContainsKey(Environment.ConnectionStringName))
            {
                props.Remove(Environment.ConnectionStringName);
            }

            // This line is only for TFS integration build MSTest runner
            ForceLoadingAssembliesForMsTestRunner();

            _sessionFactory = _configuration.BuildSessionFactory();
        }
Example #2
0
        private static void Main()
        {
            var appConfig = new AppSettingsConfiguration();
            var connectionStringBuilder = new SqlConnectionStringBuilder();

            connectionStringBuilder.DataSource         = appConfig.GetProperty("DatabaseServerName");
            connectionStringBuilder.IntegratedSecurity = true;

            var dbCreated    = false;
            var databaseName = appConfig.GetProperty("DatabaseCatalogName");
            var sqlDataPath  = String.Empty; // appConfig.GetProperty("SqlDataPath");

            using (var sqlConnnection = new SqlConnection(connectionStringBuilder.ConnectionString))
            {
                sqlConnnection.Open();

                sqlDataPath = GetDbFilePath(sqlConnnection, "master");

                if (!CheckDbExists(sqlConnnection, databaseName))
                {
                    CreateDatabase(sqlConnnection, databaseName, sqlDataPath);
                    dbCreated = true;
                }
            }
            var appSettingsConfiguration = new AppSettingsConfiguration();

            Console.WriteLine("Creating schema script....");
            var msSql2008PersistenceConfigurerProvider = new MsSql2008PersistenceConfigurerProvider(appSettingsConfiguration);

#if DEBUG
            FluentConfigurationSerializer.IsEnabled = false;
#endif
            IConfigurationProvider fluentConfigurationProvider = new FluentConfigurationProvider(msSql2008PersistenceConfigurerProvider, new AssemblyLocator());
            var configuration = fluentConfigurationProvider.Configure().GetEnumerator().Current;
            ISessionFactoryProvider sessionFactoryProvider = new SessionFactoryProvider(fluentConfigurationProvider);
            var sessionFactory = sessionFactoryProvider.GetFactory(null);

            var exportFileName = appConfig.GetProperty(ExportOutputFilePropertyName);

            var sb = new StringBuilder();

            var headerComment = string.Format(
                "{0}{0}{1}{0}{2}{0}{3}{0}",
                Environment.NewLine,
                "/***********************************************************",
                "Begin: SQL Scripts to create RemGenDatabase database objects",
                "***********************************************************/"
                );
            sb.Append(headerComment);

            var sql = GenerateSchemaCreationSql();

            using (var session = sessionFactory.OpenSession())
            {
                TextWriter textWriter = new StringWriter();

                new SchemaExport(configuration).Execute(
                    true,
                    false,
                    false,
                    session.Connection,
                    textWriter);
                sql += textWriter.ToString();
            }

            sb.Append(sql);
            sb.Append(Environment.NewLine);
            sb.Append("/***********************************************************");
            sb.Append(Environment.NewLine);
            sb.Append("End: SQL Scripts to create RemGenDatabase database objects");
            sb.Append(Environment.NewLine);
            sb.Append("***********************************************************/");
            sb.Append(Environment.NewLine);
            sb.Append(Environment.NewLine);

            Debug.Write(sb.ToString());
            Console.WriteLine(sb.ToString());

            using (var fs = new FileStream(exportFileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (var streamWriter = new StreamWriter(fs))
                {
                    streamWriter.Write(sb.ToString());
                }
            }
            Console.WriteLine("Deploy to Database (Y/N)?");
            var answer = Console.ReadLine();
            if (string.Compare(answer, "yes", true) == 0 || string.Compare(answer, "y", true) == 0)
            {
                if (!dbCreated)
                {
                    Console.WriteLine("Dropping Database...");
                    using (var sqlConnection = new SqlConnection(connectionStringBuilder.ConnectionString))
                    {
                        sqlConnection.Open();
                        using (
                            var sqlDropDatabaseCommand =
                                new SqlCommand(
                                    string.Format(
                                        "USE {0} ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE USE master DROP DATABASE {0}",
                                        databaseName),
                                    sqlConnection))
                        {
                            sqlDropDatabaseCommand.ExecuteNonQuery();
                        }
                    }
                }
                using (var sqlConnection = new SqlConnection(connectionStringBuilder.ConnectionString))
                {
                    sqlConnection.Open();
                    if (!dbCreated)
                    {
                        CreateDatabase(sqlConnection, databaseName, sqlDataPath);
                    }
                }
                //connectionStringBuilder.InitialCatalog = databaseName;
                using (var sqlConnection = new SqlConnection(connectionStringBuilder.ConnectionString))
                {
                    sqlConnection.Open();
                    sqlConnection.ChangeDatabase(databaseName);
                    Console.WriteLine("Running schema script...");
                    var fullScript = sb.ToString();
                    var goSplit    = fullScript.Split(
                        new[] { string.Format("go {0}", Environment.NewLine) }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var sqlstring in goSplit)
                    {
                        var sqlToRun = sqlstring;
                        if (sqlstring == goSplit[0])
                        {
                            sqlToRun = sqlstring.Replace(headerComment, "");
                        }
                        using (var createSchemaCommand = new SqlCommand(sqlToRun, sqlConnection))
                        {
                            createSchemaCommand.ExecuteNonQuery();
                        }
                    }
                }
            }
        }