/// <summary>Creates a new instance of the <see cref="ConfigurationBuilder"/>.</summary>
        public ConfigurationBuilder(IDefinitionProvider[] definitionProviders, ClassMappingGenerator generator, IWebContext webContext, ConfigurationBuilderParticipator[] participators, DatabaseSection config, ConnectionStringsSection connectionStrings)
        {
            this.definitionProviders = definitionProviders;
            this.generator = generator;
            this.webContext = webContext;
            this.participators = participators;

            if (config == null) config = new DatabaseSection();

            if (!string.IsNullOrEmpty(config.HibernateMapping))
                DefaultMapping = config.HibernateMapping;

            SetupProperties(config, connectionStrings);
            SetupMappings(config);

            TryLocatingHbmResources = config.TryLocatingHbmResources;
            tablePrefix = config.TablePrefix;
            batchSize = config.BatchSize;
            childrenLaziness = config.ChildrenLaziness;
        }
        private void SetupFlavourProperties(DatabaseSection config, ConnectionStringsSection connectionStrings)
        {
            DatabaseFlavour flavour = config.Flavour;
            if (flavour == DatabaseFlavour.AutoDetect)
            {
                ConnectionStringSettings css = connectionStrings.ConnectionStrings[config.ConnectionStringName];
                if (css == null)
                    throw new ConfigurationErrorsException("Could not find the connection string named '" + config.ConnectionStringName + "' that was defined in the n2/database configuration section.");
                flavour = DetectFlavor(css);
            }

            // HACK: used to support seamless text/nvarchar(max) support across databases
            if (flavour == DatabaseFlavour.MySql)
                stringLength = 16777215;

            switch (flavour)
            {
                case DatabaseFlavour.SqlServer2000:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2000Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.SqlServer:
                case DatabaseFlavour.SqlServer2005:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.SqlServer2008:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.SqlCe3:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCeDialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.SqlCe:
                case DatabaseFlavour.SqlCe4:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MsSqlCe40Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.MySql:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.MySqlDataDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.MySQL5Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.SqLite:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.SQLite20Driver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.SQLiteDialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.Firebird:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.FirebirdDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.FirebirdDialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.Generic:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OleDbDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.GenericDialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.Jet:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver";
                    Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.JetDriver.JetDialect, NHibernate.JetDriver";
                    break;
                case DatabaseFlavour.DB2:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OdbcDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.DB2Dialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.Oracle9i:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle9iDialect).AssemblyQualifiedName;
                    break;
                case DatabaseFlavour.Oracle:
                case DatabaseFlavour.Oracle10g:
                    Properties[NHibernate.Cfg.Environment.ConnectionDriver] = typeof(NHibernate.Driver.OracleClientDriver).AssemblyQualifiedName;
                    Properties[NHibernate.Cfg.Environment.Dialect] = typeof(NHibernate.Dialect.Oracle10gDialect).AssemblyQualifiedName;
                    break;
                default:
                    throw new ConfigurationErrorsException("Couldn't determine database flavour. Please check the 'flavour' attribute of the n2/database configuration section.");
            }
        }
 void System.Configuration.Internal.IInternalConfigSystem.RefreshConfig(string sectionName) {
     if(sectionName == ConnectionStringsSectionName)
         this.connectionStringsSection = null;
     internalConfigSystem.RefreshConfig(sectionName);
 }
        /// <summary>Sets properties configuration dictionary based on configuration in the database section.</summary>
        /// <param name="config">The database section configuration.</param>
        /// <param name="connectionStrings">Connection strings from configuration</param>
        protected void SetupProperties(DatabaseSection config, ConnectionStringsSection connectionStrings)
        {
            NHibernate.Cfg.Environment.UseReflectionOptimizer = Utility.GetTrustLevel() > System.Web.AspNetHostingPermissionLevel.Medium;

            // connection

            Properties[NHibernate.Cfg.Environment.ConnectionStringName] = config.ConnectionStringName;
            Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";
            Properties[NHibernate.Cfg.Environment.Hbm2ddlKeyWords] = "none";

            SetupFlavourProperties(config, connectionStrings);

            SetupCacheProperties(config);

            // custom config properties

            foreach (string key in config.HibernateProperties.AllKeys)
            {
                Properties[key] = config.HibernateProperties[key].Value;
            }
        }
 System.Configuration.ConnectionStringsSection CreateConnectionStringsSection() {
     var internalConnectionStringsSection = (System.Configuration.ConnectionStringsSection)internalConfigSystem.GetSection(ConnectionStringsSectionName);
     var connectionStringsSection = new System.Configuration.ConnectionStringsSection();
     foreach(var connectionStringSettings in internalConnectionStringsSection.ConnectionStrings.Cast<System.Configuration.ConnectionStringSettings>()) {
         var patchedConnectionStringSettings = new System.Configuration.ConnectionStringSettings(
             connectionStringSettings.Name,
             DbEngineDetector.PatchConnectionString(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName == EntityFrameworkConnectionStringProviderName),
             connectionStringSettings.ProviderName
         );
         connectionStringsSection.ConnectionStrings.Add(patchedConnectionStringSettings);
     }
     return connectionStringsSection;
 }