/// <summary> /// /// </summary> /// <param name="config"></param> protected override void PostProcessConfiguration(Configuration config) { if (FluentNhibernateMappingAssemblies != null) { foreach (var assemblyName in FluentNhibernateMappingAssemblies) { config.AddMappingsFromAssembly(Assembly.Load(assemblyName)); } } config.Properties.Add("nhibernate.envers.KotikoBlog_with_modified_flag", "true"); //log property data for revisions config.IntegrateWithEnvers(new AttributeConfiguration()); config.SetListener(ListenerType.PreInsert, new KotikoBlogAuditEventListener()); config.SetListener(ListenerType.PreUpdate, new KotikoBlogAuditEventListener()); config.SetListener(ListenerType.PreDelete, new KotikoBlogAuditEventListener()); config.SetListener(ListenerType.PreCollectionRecreate, new KotikoBlogAuditEventListener()); config.SetListener(ListenerType.PreCollectionUpdate, new KotikoBlogAuditEventListener()); config.SetListener(ListenerType.PreCollectionRemove, new KotikoBlogAuditEventListener()); config.Cache(c => { c.UseMinimalPuts = true; c.UseQueryCache = true; c.Provider<SysCacheProvider>(); }); }
public virtual void Apply(Configuration configuration, IDatabaseProvider databaseProvider) { configuration.CurrentSessionContext<CallSessionContext>(); configuration.DataBaseIntegration(db => SetDatabase(db, databaseProvider, configuration)); configuration.Cache(SetCache); configuration.Proxy(SetProxy); }
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 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(); }
internal static ISessionFactory CreateSessionFactory() { string connectionString = ConnectionString; ModelMapper mapper = new ModelMapper(); mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes()); //mapper.BeforeMapSet += (mi, t, map) => //{ // map.BatchSize(20); // map.Cascade(Cascade.All | Cascade.DeleteOrphans); //}; HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); var configuration = new Configuration(); configuration.DataBaseIntegration(c => { c.Dialect<MsSqlCe40Dialect>(); c.ConnectionString = connectionString; c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; c.BatchSize = 25; //c.SchemaAction = SchemaAutoAction.Update; }); configuration.Cache(c => { //c.Provider<NHibernate.Caches.SysCache.SysCacheProvider>(); c.UseQueryCache = true; c.DefaultExpiration = 10000; }); configuration.AddMapping(domainMapping); try { return configuration.BuildSessionFactory(); } catch (Exception) { throw; } }
public static NHibernate.ISessionFactory BuildSessionFactory( string connectionString, bool useCache, bool useUnitTest = false) { var mapper = new PostgresNamingConventionAutomapper(useCache); // NHibernate.Mapping.ByCode.ConventionModelMapper(); var cfg = new NHibernate.Cfg.Configuration(); cfg.DataBaseIntegration(c => { // PostgreSQL c.Driver <AspNetCoreExample.Infrastructure.NHibernateNpgsqlInfra.NpgsqlDriverExtended>(); c.Dialect <AspNetCoreExample.Infrastructure.NHibernateInfra.PostgresDialectExtension>(); c.ConnectionString = connectionString; c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; #if DEBUG c.LogSqlInConsole = true; c.LogFormattedSql = true; #endif }); System.Collections.Generic.IEnumerable <System.Type> entities = typeof(AspNetCoreExample.Ddd.IdentityDomain.User) .Assembly.GetExportedTypes() .Where(x => !(x.IsAbstract && x.IsSealed)); // exclude static NHibernate.Cfg.MappingSchema.HbmMapping mapping = mapper.CompileMappingFor(entities); // The class name Template exits on both namespace RichDomainModel.Document and RichDomainModel.Notification // Solution found on: // http://stackoverflow.com/questions/1156281/nhibernate-duplicatemappingexception-when-two-classes-have-the-same-name-but-dif mapping.autoimport = false; cfg.AddMapping(mapping); // http://www.ienablemuch.com/2013/06/multilingual-and-caching-on-nhibernate.html //var filterDef = new NHibernate.Engine.FilterDefinition("lf", /*default condition*/ null, // new Dictionary<string, NHibernate.Type.IType> // { // { "LanguageCultureCode", NHibernate.NHibernateUtil.String} // }, useManyToOne: false); //cfg.AddFilterDefinition(filterDef); if (useCache) { cfg.Cache(x => { // SysCache is not stable on unit testing if (!useUnitTest) { x.Provider <NHibernate.Caches.CoreMemoryCache.CoreMemoryCacheProvider>(); // I don't know why SysCacheProvider is not stable on simultaneous unit testing, // might be SysCacheProvider is just giving one session factory, so simultaneous test see each other caches // This solution doesn't work: http://stackoverflow.com/questions/700043/mstest-executing-all-my-tests-simultaneously-breaks-tests-what-to-do } else { x.Provider <NHibernate.Caches.CoreMemoryCache.CoreMemoryCacheProvider>(); } // http://stackoverflow.com/questions/2365234/how-does-query-caching-improves-performance-in-nhibernate // Need to be explicitly turned on so the .Cacheable directive on Linq will work: x.UseQueryCache = true; }); } ////// temporarily set to true so we can see the SQL when we are in web, when we are not in unit test. //if (true || useUnitTest) //{ // cfg.SetInterceptor(new NHSQLInterceptor()); //} cfg.LinqToHqlGeneratorsRegistry < AspNetCoreExample.Infrastructure.NHibernateInfra.PostgresLinqToHqlGeneratorsRegistry >(); var sf = cfg.BuildSessionFactory(); return(sf); }
public static NHibernate.ISessionFactory BuildSessionFactory(bool useUnitTest = false) { var mapper = new NHibernate.Mapping.ByCode.ConventionModelMapper(); mapper.IsEntity((t, declared) => t.Namespace == "Domain"); mapper.BeforeMapClass += mapper_BeforeMapClass; mapper.BeforeMapProperty += mapper_BeforeMapProperty; mapper.BeforeMapManyToOne += mapper_BeforeMapManyToOne; mapper.BeforeMapBag += mapper_BeforeMapBag; var cfg = new NHibernate.Cfg.Configuration(); // .DatabaseIntegration! Y U EXTENSION METHOD?! cfg.DataBaseIntegration(c => { var cs = System.Configuration.ConfigurationManager.ConnectionStrings["TheSpaConnection"].ConnectionString; // SQL Server c.Driver <NHibernate.Driver.SqlClientDriver>(); c.Dialect <NHibernate.Dialect.MsSql2008Dialect>(); c.ConnectionString = "Server=.;Database=SpaArchitectureMvp;Trusted_Connection=True"; //// PostgreSQL //c.Driver<NHibernate.Driver.NpgsqlDriver>(); //c.Dialect<NHibernate.Dialect.PostgreSQLDialect>(); //c.ConnectionString = cs; if (useUnitTest) { c.LogSqlInConsole = true; c.LogFormattedSql = true; } }); System.Collections.Generic.IEnumerable <System.Type> entities = typeof(Domain.PersonDomain.Person).Assembly.GetExportedTypes() .Where(x => !(x.IsAbstract && x.IsSealed)); // exclude static NHibernate.Cfg.MappingSchema.HbmMapping mapping = mapper.CompileMappingFor(entities); cfg.AddMapping(mapping); // http://www.ienablemuch.com/2013/06/multilingual-and-caching-on-nhibernate.html //var filterDef = new NHibernate.Engine.FilterDefinition("lf", /*default condition*/ null, // new Dictionary<string, NHibernate.Type.IType> // { // { "LanguageCultureCode", NHibernate.NHibernateUtil.String} // }, useManyToOne: false); //cfg.AddFilterDefinition(filterDef); cfg.Cache(x => { // SysCache is not stable on unit testing if (!useUnitTest) { x.Provider <NHibernate.Caches.SysCache.SysCacheProvider>(); // I don't know why SysCacheProvider is not stable on simultaneous unit testing, // might be SysCacheProvider is just giving one session factory, so simultaneous test see each other caches // This solution doesn't work: http://stackoverflow.com/questions/700043/mstest-executing-all-my-tests-simultaneously-breaks-tests-what-to-do } else { // This is more stable in unit testing x.Provider <NHibernate.Cache.HashtableCacheProvider>(); } // http://stackoverflow.com/questions/2365234/how-does-query-caching-improves-performance-in-nhibernate // Need to be explicitly turned on so the .Cacheable directive on Linq will work: x.UseQueryCache = true; }); if (useUnitTest) { cfg.SetInterceptor(new NHSQLInterceptor()); } //new NHibernate.Tool.hbm2ddl.SchemaUpdate(cfg).Execute(useStdOut: false, doUpdate: true); //using (var file = new System.IO.FileStream(@"c:\x\ddl.txt", // System.IO.FileMode.Create, // System.IO.FileAccess.ReadWrite)) //using (var sw = new System.IO.StreamWriter(file)) //{ // new SchemaUpdate(cfg) // .Execute(sw.Write, false); //} var sf = cfg.BuildSessionFactory(); return(sf); }
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); }
public static NHibernate.ISessionFactory BuildSessionFactory(bool useUnitTest = false) { var mapper = new NHibernate.Mapping.ByCode.ConventionModelMapper(); mapper.IsEntity((t, declared) => t.Namespace == "Domain"); mapper.BeforeMapClass += mapper_BeforeMapClass; mapper.BeforeMapProperty += mapper_BeforeMapProperty; mapper.BeforeMapManyToOne += mapper_BeforeMapManyToOne; mapper.BeforeMapBag += mapper_BeforeMapBag; var cfg = new NHibernate.Cfg.Configuration(); // .DatabaseIntegration! Y U EXTENSION METHOD?! cfg.DataBaseIntegration(c => { var cs = System.Configuration.ConfigurationManager.ConnectionStrings["TheSpaConnection"].ConnectionString; // SQL Server c.Driver<NHibernate.Driver.SqlClientDriver>(); c.Dialect<NHibernate.Dialect.MsSql2008Dialect>(); c.ConnectionString = "Server=.;Database=SpaArchitectureMvp;Trusted_Connection=True"; //// PostgreSQL //c.Driver<NHibernate.Driver.NpgsqlDriver>(); //c.Dialect<NHibernate.Dialect.PostgreSQLDialect>(); //c.ConnectionString = cs; if (useUnitTest) { c.LogSqlInConsole = true; c.LogFormattedSql = true; } }); System.Collections.Generic.IEnumerable<System.Type> entities = typeof(Domain.PersonDomain.Person).Assembly.GetExportedTypes() .Where(x => !(x.IsAbstract && x.IsSealed)); // exclude static NHibernate.Cfg.MappingSchema.HbmMapping mapping = mapper.CompileMappingFor(entities); cfg.AddMapping(mapping); // http://www.ienablemuch.com/2013/06/multilingual-and-caching-on-nhibernate.html //var filterDef = new NHibernate.Engine.FilterDefinition("lf", /*default condition*/ null, // new Dictionary<string, NHibernate.Type.IType> // { // { "LanguageCultureCode", NHibernate.NHibernateUtil.String} // }, useManyToOne: false); //cfg.AddFilterDefinition(filterDef); cfg.Cache(x => { // SysCache is not stable on unit testing if (!useUnitTest) { x.Provider<NHibernate.Caches.SysCache.SysCacheProvider>(); // I don't know why SysCacheProvider is not stable on simultaneous unit testing, // might be SysCacheProvider is just giving one session factory, so simultaneous test see each other caches // This solution doesn't work: http://stackoverflow.com/questions/700043/mstest-executing-all-my-tests-simultaneously-breaks-tests-what-to-do } else { // This is more stable in unit testing x.Provider<NHibernate.Cache.HashtableCacheProvider>(); } // http://stackoverflow.com/questions/2365234/how-does-query-caching-improves-performance-in-nhibernate // Need to be explicitly turned on so the .Cacheable directive on Linq will work: x.UseQueryCache = true; }); if (useUnitTest) cfg.SetInterceptor(new NHSQLInterceptor()); //new NHibernate.Tool.hbm2ddl.SchemaUpdate(cfg).Execute(useStdOut: false, doUpdate: true); //using (var file = new System.IO.FileStream(@"c:\x\ddl.txt", // System.IO.FileMode.Create, // System.IO.FileAccess.ReadWrite)) //using (var sw = new System.IO.StreamWriter(file)) //{ // new SchemaUpdate(cfg) // .Execute(sw.Write, false); //} var sf = cfg.BuildSessionFactory(); return sf; }
private ISessionFactory CreateNhSessionFactory(string connectionString) { var nhConfiguration = new Configuration(); nhConfiguration.Cache(properties => properties.Provider <HashtableCacheProvider>()); if (connectionString.ToUpper().Contains("SERVER=")) { //is sql server nhConfiguration.DataBaseIntegration(dbi => { dbi.Dialect <MsSql2008Dialect>(); dbi.Driver <Sql2008ClientDriver>(); dbi.ConnectionProvider <DriverConnectionProvider>(); dbi.IsolationLevel = IsolationLevel.ReadCommitted; dbi.ConnectionString = connectionString; dbi.Timeout = 60; dbi.LogFormattedSql = false; dbi.LogSqlInConsole = false; }); } else if (connectionString.ToUpper().Contains("MICROSOFT.ACE.OLEDB") || connectionString.ToUpper().Contains("MICROSOFT.JET.OLEDB")) { //is OLE DB nhConfiguration.DataBaseIntegration(dbi => { dbi.Dialect <GenericDialect>(); dbi.Driver <OleDbDriver>(); dbi.ConnectionProvider <DriverConnectionProvider>(); dbi.IsolationLevel = IsolationLevel.ReadCommitted; dbi.ConnectionString = connectionString; dbi.Timeout = 60; dbi.LogFormattedSql = false; dbi.LogSqlInConsole = false; }); } else if (connectionString.ToUpper().EndsWith(".DB")) { //is sql lite nhConfiguration.DataBaseIntegration(dbi => { dbi.Dialect <SQLiteDialect>(); dbi.Driver <SQLite20Driver>(); dbi.ConnectionProvider <DriverConnectionProvider>(); dbi.IsolationLevel = IsolationLevel.ReadCommitted; dbi.ConnectionString = connectionString; dbi.Timeout = 60; dbi.LogFormattedSql = false; dbi.LogSqlInConsole = false; }); } else { //must be oracle nhConfiguration.DataBaseIntegration(dbi => { dbi.Dialect <Oracle10gDialect>(); dbi.Driver <OracleManagedDataClientDriver>(); dbi.ConnectionProvider <DriverConnectionProvider>(); dbi.IsolationLevel = IsolationLevel.ReadCommitted; dbi.ConnectionString = connectionString; dbi.Timeout = 60; dbi.LogFormattedSql = false; dbi.LogSqlInConsole = false; }); } nhConfiguration.Properties["show_sql"] = "false"; var sessionFactory = nhConfiguration.BuildSessionFactory(); return(sessionFactory); }
/// <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); }
/// <summary> /// This method attempts to find a session factory stored in <see cref="sessionFactories" /> /// via its name; if it can't be found it creates a new one and adds it the hashtable. /// </summary> /// <param name="sessionFactoryConfigPath">Path location of the factory config</param> public ISessionFactory GetSessionFactoryFor(string sessionFactoryConfigPath) { Check.Require(!string.IsNullOrEmpty(sessionFactoryConfigPath), "sessionFactoryConfigPath may not be null nor empty"); // Attempt to retrieve a stored SessionFactory from the hashtable. ISessionFactory sessionFactory = (ISessionFactory)sessionFactories[sessionFactoryConfigPath]; // Failed to find a matching SessionFactory so make a new one. if (sessionFactory == null) { Check.Require(File.Exists(sessionFactoryConfigPath), "The config file at '" + sessionFactoryConfigPath + "' could not be found"); Configuration cfg = new Configuration(); cfg.Configure(sessionFactoryConfigPath); var map = GetMappings(sessionFactoryConfigPath); cfg.AddDeserializedMapping(map, "NHSchemaTest"); //InitializeEventListeners (cfg); cfg.Cache(u => { u.UseQueryCache = true; u.RegionsPrefix = "LongTerm"; }).SessionFactory().Caching.Through<SysCacheProvider>(); // Now that we have our Configuration object, create a new SessionFactory sessionFactory = cfg.BuildSessionFactory(); if (sessionFactory == null) { throw new InvalidOperationException("cfg.BuildSessionFactory() returned null."); } if (null == configurations) configurations = new Dictionary<string, Configuration>(); configurations[sessionFactoryConfigPath] = cfg; sessionFactories.Add(sessionFactoryConfigPath, sessionFactory); //HttpRuntime.Cache.Add(sessionFactoryConfigPath,sessionFactory, null, DateTime.Now.AddDays(7), // TimeSpan.Zero, CacheItemPriority.High, null); } return sessionFactory; }
// public class Order // { // public int Id { get; set; } // public Customer Customer { get; set; } // } static void InitializeNHibernate() { NHibernateProfiler.Initialize(); cfg = new Configuration(); cfg.DataBaseIntegration(x => { x.ConnectionStringName = "adventureworks"; x.Driver<SqlClientDriver>(); x.Dialect<MsSql2008Dialect>(); x.IsolationLevel = IsolationLevel.ReadCommitted; x.Timeout = 10; }); cfg.SessionFactoryName("AdventureWorks"); cfg.Cache(x => { x.Provider<HashtableCacheProvider>(); x.UseQueryCache = true; }); cfg.SessionFactory().GenerateStatistics(); cfg.AddAssembly(typeof (Customer).Assembly); }
public static Configuration Initialize() { ModelMapper mapper = new ModelMapper(); mapper.AddMappings(Assembly.GetAssembly(typeof(NHibernateSessionManager)).GetExportedTypes()); Configuration cfg = new Configuration(); cfg.DataBaseIntegration(c => { c.Driver<SqlServerCeDriver>(); c.Dialect<MsSqlCe40Dialect>(); c.LogSqlInConsole = true; c.LogFormattedSql = true; c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; c.ConnectionString = ConnectionString; }); cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); cfg.CurrentSessionContext<ThreadStaticSessionContext>(); // Set up second level caching cfg.Cache(props => { props.Provider<AppFabricProvider>(); props.UseQueryCache = true; }); // (Re-)create the database. new SchemaExport(cfg).Execute(false, true, false); _sessionFactory = cfg.BuildSessionFactory(); return cfg; }