public static ISession OpenSession() { if (sessionFactory == null) { System.Collections.Specialized.NameValueCollection sets = System.Configuration.ConfigurationManager.AppSettings; //获取连接字符串 string server = Utilities.GetConfigValue("server"); string pwd = VTMS.Common.Utilities.Base64Dencrypt(Utilities.GetConfigValue("DBPassword")); string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); connectionString = string.Format(connectionString, server, pwd); try { Configuration cfg = new Configuration().Configure(); cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>()); cfg.DataBaseIntegration(db => { db.ConnectionString = connectionString; }); sessionFactory = cfg.BuildSessionFactory(); } catch (Exception e) { VTMS.Common.MessageUtil.ShowError("无法登陆服务器,请检查服务器IP设置是否正确,错误信息为:" + e.Message); } } return sessionFactory.OpenSession(); }
public Configuration BuildConfiguration(string connectionString, string sessionFactoryName) { Contract.Requires(!string.IsNullOrEmpty(connectionString), "ConnectionString is null or empty"); Contract.Requires(!string.IsNullOrEmpty(sessionFactoryName), "SessionFactory name is null or empty"); Contract.Requires(!string.IsNullOrEmpty(_databaseSchema), "Database Schema is null or empty"); Contract.Requires(_configurator != null, "Configurator is null"); return CatchExceptionHelper.TryCatchFunction( () => { DomainTypes = GetTypeOfEntities(_assemblies); if (DomainTypes == null) throw new Exception("Type of domains is null"); var configure = new Configuration(); configure.SessionFactoryName(sessionFactoryName); configure.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()); configure.DataBaseIntegration(db => GetDatabaseIntegration(db, connectionString)); if (_configurator.GetAppSettingString("IsCreateNewDatabase").ConvertToBoolean()) { configure.SetProperty("hbm2ddl.auto", "create-drop"); } configure.Properties.Add("default_schema", _databaseSchema); configure.AddDeserializedMapping(GetMapping(), _configurator.GetAppSettingString("DocumentFileName")); SchemaMetadataUpdater.QuoteTableAndColumns(configure); return configure; }, Logger); }
public void PrepareSessionFactory() { Configuration = new Configuration(); Configuration.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "db"; db.Dialect<MsSql2008Dialect>(); }); Configuration.SetProperty("show_sql", "true"); Configuration.SetDefaultAssembly("NHibernateDeepDive"); Configuration.SetDefaultNamespace("NHibernate_Deep_Dive.Entities"); Configuration.AddXmlFile("ClearDatabaseScript.hbm.xml"); foreach (var mappingFile in Directory.GetFiles(MappingsDirectory)) { Configuration.AddXmlFile(mappingFile); } AdjustConfiguration(Configuration); Configuration.SessionFactory().GenerateStatistics(); SessionFactory = Configuration.BuildSessionFactory(); //new SchemaExport(Configuration).Drop(false, true); new SchemaExport(Configuration).Execute(false, true, false); BeforeTestRun(); PopulateDatabase(); HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); }
public static Configuration Initialize() { INHibernateConfigurationCache cache = new NHibernateConfigurationFileCache(); var mappingAssemblies = new[] { typeof(ActionConfirmation<>).Assembly.GetName().Name }; var configuration = cache.LoadConfiguration(CONFIG_CACHE_KEY, null, mappingAssemblies); if (configuration == null) { configuration = new Configuration(); configuration .Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "DonSharpLiteConnectionString"; db.Dialect<MsSql2008Dialect>(); }) .AddAssembly(typeof(ActionConfirmation<>).Assembly) .CurrentSessionContext<LazySessionContext>(); var mapper = new ConventionModelMapper(); mapper.WithConventions(configuration); cache.SaveConfiguration(CONFIG_CACHE_KEY, configuration); } return configuration; }
private static void Main() { NHibernateProfiler.Initialize(); var cfg = new Configuration(); cfg.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "scratch"; db.Dialect<MsSql2008Dialect>(); db.BatchSize = 500; }) .AddAssembly(Assembly.GetExecutingAssembly()) .SessionFactory().GenerateStatistics(); new SchemaExport(cfg).Execute(script: false, export: true, justDrop: false); var sessionFactory = cfg.BuildSessionFactory(); using(var session = sessionFactory.OpenSession()) using(var tx = session.BeginTransaction()) { // NHibernate code goes here tx.Commit(); } Console.WriteLine("Press <ENTER> to exit..."); Console.ReadLine(); }
public BasicConfiguration(IConnectionString connectionString) { _configuration = new Configuration(); _configuration.Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionString = connectionString.FormattedConnectionString; db.Dialect<MsSql2008Dialect>(); }) .AddAssembly(typeof(NHTestEntity).Assembly); }
public virtual void Apply(Configuration configuration, IDatabaseProvider databaseProvider) { configuration.CurrentSessionContext<CallSessionContext>(); configuration.DataBaseIntegration(db => SetDatabase(db, databaseProvider, configuration)); configuration.Cache(SetCache); configuration.Proxy(SetProxy); }
public static Configuration ConfigureNHibernate() { var configure = new Configuration(); configure.SessionFactoryName("Demo"); configure.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()); configure.DataBaseIntegration(db => { db.Dialect<MsSql2008Dialect>(); db.Driver<SqlClientDriver>(); db.ConnectionString = ConnectionString; db.LogSqlInConsole = true;//用于显示输出SQL }); return configure; }
public static Configuration CreateConfiguration() { var configuration = new Configuration(); configuration .Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "DefaultConnection"; db.Dialect<MsSql2008Dialect>(); }) .AddAssembly(typeof(Entity).Assembly) .CurrentSessionContext<LazySessionContext>(); return configuration; }
public static Configuration Initialize() { Configuration configuration = new Configuration(); configuration .Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "CaTSConnectionString"; db.Dialect<MsSql2008Dialect>(); }) .AddAssembly(typeof(ActionConfirmation<>).Assembly) .CurrentSessionContext<LazySessionContext>(); ConventionModelMapper mapper = new ConventionModelMapper(); mapper.WithConventions(configuration); return configuration; }
private static NHibernate.ISessionFactory Init() { var config = new Configuration(); config.Proxy(proxy => proxy.ProxyFactoryFactory<DefaultProxyFactoryFactory>()); config.DataBaseIntegration(db => { db.Dialect<SQLiteDialect>(); db.ConnectionString = @"Data Source=|DataDirectory|\Test.db"; //db.Driver<SQLite20Driver>(); db.SchemaAction = SchemaAutoAction.Create; db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; }); config.AddDeserializedMapping(InternalHelper.GetAllMapper(), "Models"); return config.BuildSessionFactory(); }
public static Configuration GetConfiguration() { var configure = new Configuration(); configure.SessionFactoryName("NHibernateDB"); //Proxy扩展方法用于配置NHibernate延迟加载的字节码提供程序 configure.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()); configure.DataBaseIntegration(db => { db.Dialect<MsSql2005Dialect>(); db.Driver<SqlClientDriver>(); db.ConnectionString = _ConnectionString; }); return configure; }
private ISessionFactory BuildSessionFactory() { var configuration = new Configuration(); configuration.DataBaseIntegration(db => { db.Dialect<MsSql2008Dialect>(); db.ConnectionStringName = "NHibernate_CRUD"; }); configuration.Properties[Environment.CurrentSessionContextClass] = typeof(LazySessionContext).AssemblyQualifiedName; configuration.Cache(c => c.UseQueryCache = false); configuration.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()); configuration.AddAssembly(GetType().Assembly); return configuration.BuildSessionFactory(); }
protected virtual void RegisterProperties(NHibernate.Cfg.Configuration configuration) { configuration.Proxy(p => p.ProxyFactoryFactory <ComponentProxyFactoryFactory>()); configuration.DataBaseIntegration(db => { db.Dialect <MsSql2005Dialect>(); db.Driver <SqlClientDriver>(); //db.ConnectionStringName = "ConnectionString"; db.ConnectionString = Common.Constants.AppConfig.ConnectionString; db.BatchSize = 10; }); configuration.CurrentSessionContext <ThreadLocalConversationalSessionContext>(); configuration.Cache(cp => { cp.UseQueryCache = true; cp.Provider <SysCacheProvider>(); }); }
private static NHibernate.ISessionFactory Initialize(Action<ModelMapper> action) { var config = new Configuration(); config.Proxy(proxy => proxy.ProxyFactoryFactory<DefaultProxyFactoryFactory>()); config.DataBaseIntegration(db => { db.Dialect<SQLiteDialect>(); db.ConnectionString = @"Data Source=|DataDirectory|\Test.db"; //db.Driver<SQLite20Driver>(); db.SchemaAction = SchemaAutoAction.Create; db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; }); var mapper = new ModelMapper(); action(mapper); var maps = mapper.CompileMappingForAllExplicitlyAddedEntities(); config.AddDeserializedMapping(maps, "Models"); return config.BuildSessionFactory(); }
public static Configuration Initialize() { var configuration = new Configuration(); configuration .Proxy(p => p.ProxyFactoryFactory<DefaultProxyFactoryFactory>()) .DataBaseIntegration(db => { db.ConnectionStringName = "DevelopmentStack"; db.Dialect<MsSql2008Dialect>(); db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; }) .AddAssembly(typeof (Stack).Assembly) .CurrentSessionContext<LazySessionContext>(); ConventionModelMapper mapper = new ConventionModelMapper(); mapper.WithConventions(configuration); return configuration; }
public Configuration ConfigureNHibernate() { var configure = new Configuration(); configure.SessionFactoryName("Demo"); configure.Proxy(p => { p.Validation = false; p.ProxyFactoryFactory<ProxyFactoryFactory>(); }); configure.DataBaseIntegration(db => { db.Dialect<MsSql2008Dialect>(); db.Driver<SqlClientDriver>(); db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; db.IsolationLevel = IsolationLevel.ReadCommitted; db.ConnectionString = ConnectionString; db.Timeout = 10; db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'"; }); return configure; }
private Configuration BuildConfiguration() { Configuration cfg = new Configuration(); cfg.SessionFactoryName("Sample.ReadModel"); cfg.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>()); cfg.DataBaseIntegration(db => { db.Dialect<NHibernate.Dialect.MsSql2008Dialect>(); db.Driver<NHibernate.Driver.SqlClientDriver>(); db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; db.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ReadModel"].ConnectionString; db.AutoCommentSql = true; db.LogSqlInConsole = false; db.LogFormatedSql = true; db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'"; }); cfg.AddAssembly(typeof(Person).Assembly); return cfg; }
public void FullConfiguration() { var configure = new Configuration(); configure.SessionFactoryName("SomeName"); configure.Cache(c => { c.UseMinimalPuts = true; c.DefaultExpiration = 15; c.RegionsPrefix = "xyz"; c.Provider<HashtableCacheProvider>(); c.QueryCache<StandardQueryCache>(); }); configure.CollectionTypeFactory<DefaultCollectionTypeFactory>(); configure.HqlQueryTranslator<ClassicQueryTranslatorFactory>(); configure.LinqToHqlGeneratorsRegistry<DefaultLinqToHqlGeneratorsRegistry>(); configure.Proxy(p => { p.Validation = false; p.ProxyFactoryFactory<DefaultProxyFactoryFactory>(); }); configure.Mappings(m=> { m.DefaultCatalog = "MyCatalog"; m.DefaultSchema = "MySche"; }); configure.DataBaseIntegration(db => { db.Dialect<MsSql2000Dialect>(); db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; db.Batcher<SqlClientBatchingBatcherFactory>(); db.BatchSize = 15; db.ConnectionProvider<DebugConnectionProvider>(); db.Driver<SqlClientDriver>(); db.ConnectionReleaseMode = ConnectionReleaseMode.AfterTransaction; db.IsolationLevel = IsolationLevel.ReadCommitted; db.ConnectionString = "The connection string"; db.AutoCommentSql = true; db.ExceptionConverter<SQLStateConverter>(); db.PrepareCommands = true; db.Timeout = 10; db.MaximumDepthOfOuterJoinFetching = 11; db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'"; db.SchemaAction = SchemaAutoAction.Validate; }); Assert.That(configure.Properties[Environment.SessionFactoryName], Is.EqualTo("SomeName")); Assert.That(configure.Properties[Environment.CacheProvider], Is.EqualTo(typeof(HashtableCacheProvider).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.CacheRegionPrefix], Is.EqualTo("xyz")); Assert.That(configure.Properties[Environment.QueryCacheFactory], Is.EqualTo(typeof(StandardQueryCache).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.UseMinimalPuts], Is.EqualTo("true")); Assert.That(configure.Properties[Environment.CacheDefaultExpiration], Is.EqualTo("15")); Assert.That(configure.Properties[Environment.CollectionTypeFactoryClass], Is.EqualTo(typeof(DefaultCollectionTypeFactory).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.UseProxyValidator], Is.EqualTo("false")); Assert.That(configure.Properties[Environment.ProxyFactoryFactoryClass], Is.EqualTo(typeof(DefaultProxyFactoryFactory).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.QueryTranslator], Is.EqualTo(typeof(ClassicQueryTranslatorFactory).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.DefaultCatalog], Is.EqualTo("MyCatalog")); Assert.That(configure.Properties[Environment.DefaultSchema], Is.EqualTo("MySche")); Assert.That(configure.Properties[Environment.Dialect], Is.EqualTo(typeof(MsSql2000Dialect).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.Hbm2ddlKeyWords], Is.EqualTo("auto-quote")); Assert.That(configure.Properties[Environment.BatchStrategy], Is.EqualTo(typeof(SqlClientBatchingBatcherFactory).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.BatchSize], Is.EqualTo("15")); Assert.That(configure.Properties[Environment.ConnectionProvider], Is.EqualTo(typeof(DebugConnectionProvider).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.ConnectionDriver], Is.EqualTo(typeof(SqlClientDriver).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.ReleaseConnections], Is.EqualTo(ConnectionReleaseModeParser.ToString(ConnectionReleaseMode.AfterTransaction))); Assert.That(configure.Properties[Environment.Isolation], Is.EqualTo("ReadCommitted")); Assert.That(configure.Properties[Environment.ConnectionString], Is.EqualTo("The connection string")); Assert.That(configure.Properties[Environment.UseSqlComments], Is.EqualTo("true")); Assert.That(configure.Properties[Environment.SqlExceptionConverter], Is.EqualTo(typeof(SQLStateConverter).AssemblyQualifiedName)); Assert.That(configure.Properties[Environment.PrepareSql], Is.EqualTo("true")); Assert.That(configure.Properties[Environment.CommandTimeout], Is.EqualTo("10")); Assert.That(configure.Properties[Environment.MaxFetchDepth], Is.EqualTo("11")); Assert.That(configure.Properties[Environment.QuerySubstitutions], Is.EqualTo("true 1, false 0, yes 'Y', no 'N'")); Assert.That(configure.Properties[Environment.Hbm2ddlAuto], Is.EqualTo("validate")); configure.Properties[Environment.LinqToHqlGeneratorsRegistry].Should().Be(typeof(DefaultLinqToHqlGeneratorsRegistry).AssemblyQualifiedName); }
private void ConfigureNHibernate() { _configuration = new Configuration(); _configuration.DataBaseIntegration(x => { if (_inMemory) { x.Dialect<SQLiteDialect>(); x.Driver<SQLite20Driver>(); x.ConnectionProvider<DriverConnectionProvider>(); } else x.Dialect<MsSql2008Dialect>(); x.ConnectionString = _connectionString; x.SchemaAction = SchemaAutoAction.Update; x.IsolationLevel = IsolationLevel.ReadCommitted; x.HqlToSqlSubstitutions = "True 1, False 0, true 1, false 0, yes 'Y', no 'N'"; x.BatchSize = 200; }); var mappingAssemblies = _assemblies; foreach (var mappingAssembly in mappingAssemblies.Where(mappingAssembly => !String.IsNullOrWhiteSpace(mappingAssembly))) { _configuration.AddAssembly(mappingAssembly); } _configuration.BuildMappings(); _configuration.SetProperty(NHEnvironment.CacheDefaultExpiration, 120.ToString()); _configuration.SetProperty(NHEnvironment.ShowSql, "false"); _configuration.Proxy(cfg => cfg.ProxyFactoryFactory<DefaultProxyFactoryFactory>()); _configuration.SessionFactory() .GenerateStatistics(); }
public void Initialize() { configure = new Configuration(); configure.SessionFactoryName("Demo"); configure.Proxy(p => { p.ProxyFactoryFactory<ProxyFactoryFactory>(); }); configure.DataBaseIntegration(db => { db.Dialect<MsSql2008Dialect>(); db.Driver<SqlClientDriver>(); db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; db.IsolationLevel = IsolationLevel.ReadCommitted; db.ConnectionString = ConnectionString; db.BatchSize = 20; db.Timeout = 10; db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'"; }); Map(); }
/// <summary> /// Configure NHibernate /// </summary> private void ConfigureNHibernate() { _configuration = new NHConfiguration(); _configuration.DataBaseIntegration(x => { //x.AutoCommentSql = true; //x.Dialect<MsSqlCustomDialect>(); x.Dialect<MsSqlCustomDialect>(); x.ConnectionString = _connectionString; x.SchemaAction = SchemaAutoAction.Update; x.IsolationLevel = IsolationLevel.ReadCommitted; x.HqlToSqlSubstitutions = "True 1, False 0, true 1, false 0, yes 'Y', no 'N'"; x.BatchSize = 15; }); var mappingAssemblies = _assemblies; foreach (var mappingAssembly in mappingAssemblies) { if (!String.IsNullOrWhiteSpace(mappingAssembly)) { _configuration.AddAssembly(mappingAssembly); } } _configuration.BuildMappings(); _configuration.Cache(cfg => { cfg.DefaultExpiration = 120; cfg.Provider<SysCacheProvider>(); cfg.UseMinimalPuts = true; cfg.RegionsPrefix = "xyz"; cfg.UseQueryCache = true; }); _configuration.SetProperty(NHibernate.Cfg.Environment.CacheDefaultExpiration, 120.ToString()); _configuration.SetProperty(NHibernate.Cfg.Environment.ShowSql, "false"); _configuration.Proxy(cfg => cfg.ProxyFactoryFactory<DefaultProxyFactoryFactory>()); _configuration.SessionFactory() .GenerateStatistics(); new AuditingEventListener().Register(_configuration); }