/// <summary>
        ///     Configures HiLo table used to generage entity identifiers with HiLo algorithm.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        private static void ConfigureHiLoTable(NHibernate.Cfg.Configuration configuration)
        {
            var script = new StringBuilder();

            script.AppendLine("ALTER TABLE HiLo ADD [Entity] VARCHAR(128) NULL");
            script.AppendLine("GO");

            script.AppendLine("CREATE NONCLUSTERED INDEX IX_HiLo_Entity ON HiLo (Entity ASC);");
            script.AppendLine("GO");

            foreach (var name in configuration.ClassMappings.Select(m => m.EntityName).Distinct())
            {
                script.AppendLine($"INSERT INTO [HiLo] (Entity, NextHi) VALUES ('{name}',1);");
            }

            configuration.AddAuxiliaryDatabaseObject(
                new SimpleAuxiliaryDatabaseObject(
                    script.ToString(),
                    null,
                    new HashSet <string>
            {
                typeof(MsSql2000Dialect).FullName,
                typeof(MsSql2005Dialect).FullName,
                typeof(MsSql2008Dialect).FullName,
                typeof(MsSql2012Dialect).FullName,
                typeof(MsSqlAzure2008Dialect).FullName
            }));
        }
Пример #2
0
        /// <summary>
        /// this method is used generate the sql for adding entity column and entity values to the nhibernate_identity_table
        /// </summary>
        /// <param name="config"></param>
        public static void CreateHighLowScript(NHibernate.Cfg.Configuration config)
        {
            try
            {
                var dialect      = Activator.CreateInstance(Type.GetType(config.GetProperty(NHibernate.Cfg.Environment.Dialect))) as Dialect;
                var createScript = new StringBuilder();

                createScript.AppendFormat("DELETE FROM {0};", HiLoTableName);
                createScript.AppendLine();
                createScript.AppendFormat("ALTER TABLE {0} {1} {2} {3} NOT NULL;", HiLoTableName, dialect.AddColumnString, EntityTableColumnName, dialect.GetTypeName(SqlTypeFactory.GetAnsiString(128)));
                createScript.AppendLine();
                createScript.AppendFormat("CREATE NONCLUSTERED INDEX IX_{0}_{1} ON {0} ({1} ASC);", HiLoTableName, EntityTableColumnName);
                createScript.AppendLine();
                if (dialect.SupportsSqlBatches)
                {
                    createScript.AppendLine("GO");
                    createScript.AppendLine();
                }
                foreach (var entityName in config.ClassMappings.Select(m => m.EntityName).Distinct())
                {
                    createScript.AppendFormat("INSERT INTO [{0}] ({1}, {2}) VALUES ('{3}',1);", HiLoTableName, EntityTableColumnName, TableGenerator.DefaultColumnName, entityName);
                    createScript.AppendLine();
                }
                if (dialect.SupportsSqlBatches)
                {
                    createScript.AppendLine("GO");
                    createScript.AppendLine();
                }

                config.AddAuxiliaryDatabaseObject(new SimpleAuxiliaryDatabaseObject(createScript.ToString(), null));
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
#if DEBUG
                throw ex;
#endif
            }
        }