/// <summary> /// Only development phase usage do not use production code /// </summary> /// <param name="factoryKey"></param> private void BuildSchemaByDroping(string factoryKey) { var filePath = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, factoryKey + ".NHibernate.config")?.FirstOrDefault(); if (string.IsNullOrEmpty(filePath)) { Console.WriteLine(factoryKey + ".NHibernate.config file not found for BuildSchemaDroping!"); return; } var nHibernateMappingAssembly = GetAssemblyName(filePath); var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(filePath); FluentConfiguration a = Fluently.Configure(cfg).Mappings(m => { var assembly = Assembly.Load(nHibernateMappingAssembly); m.HbmMappings.AddFromAssembly(assembly); m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly); }); SchemaExport schema = new SchemaExport(a.BuildConfiguration()); if (schema != null) { schema.Execute(true, true, false); } }
private static NhibConfigData GetOrCreateConfigData(String configFileName) { NhibConfigData retvalue = factories.SafeGet(configFileName); if (null == retvalue) { //This is the first time we ask for this configuration global::NHibernate.Cfg.Configuration config = new global::NHibernate.Cfg.Configuration(); XDocument doc = XDocument.Load(configFileName); XElement connStringElement = (from e in doc.Descendants() where e.Attribute("name") != null && e.Attribute("name").Value == "connection.connection_string" select e).Single(); String cnName = connStringElement.Value; connStringElement.Value = ConfigurationRegistry.ConnectionString(connStringElement.Value).ConnectionString; using (XmlReader reader = doc.CreateReader()) { config.Configure(reader); } ISessionFactory factory = config.BuildSessionFactory(); retvalue = new NhibConfigData() { Configuration = config, SessionFactory = factory, ConnectionName = cnName }; factories.Add(configFileName, retvalue); } return(retvalue); }
private static ISessionFactory CreateSessionFactory() { /* Configuration _cfg = null; var factory = Fluently.Configure() .Database( SQLiteConfiguration.Standard.InMemory() // .UsingFile(@"C:\Users\thiago.alves\Desktop\firstProject.db") ) .ProxyFactoryFactory(typeof(ProxyFactoryFactory)) .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Cliente>())) .ExposeConfiguration(Cfg => _cfg = Cfg) .BuildSessionFactory();*/ var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(); return Fluently.Configure(cfg) .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Cliente>()) ) .ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName) .BuildSessionFactory(); //return cfg.BuildSessionFactory(); }
private void BuildSessionFactoryFor(string configurationFilePath) { var nHibernateMappingAssembly = GetAssemblyName(configurationFilePath); ISessionFactory sessionFactory; if (nHibernateMappingAssembly != null) { var assembly = Assembly.Load(nHibernateMappingAssembly); var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(configurationFilePath); sessionFactory = Fluently.Configure(cfg).Mappings(m => { m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly); }).BuildSessionFactory(); } else { var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(configurationFilePath); sessionFactory = Fluently.Configure(cfg).BuildSessionFactory(); Fluently.Configure(cfg); } _sessionFactory = sessionFactory; Console.WriteLine("NHibernate Initiated"); }
/// <summary> /// Configures NHibernate /// </summary> public void Configure() { // Arashi.Core.NHibernate configuration = new Configuration(); configuration.Configure(); configuration = configuration.AddAssembly("Arashi.Core.NHibernate"); SessionFactory = configuration.BuildSessionFactory(); }
protected void InSession(Action <ISession> action) { var configuration = new global::NHibernate.Cfg.Configuration(); configuration.Configure(); ISessionFactory sessionFactory = configuration.BuildSessionFactory(); using (var session = sessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { action(session); transaction.Commit(); } }
public void Setup() { var configuration = new global::NHibernate.Cfg.Configuration(); configuration.Configure(); sessionFactory = configuration.BuildSessionFactory(); if (!sessionFactory.Statistics.IsStatisticsEnabled) { Assert.Ignore("Statistics should be enabled"); } categoryRepository = new CategoryRepository(sessionFactory); }
public static Configuration Configure(this Configuration configuration, XmlNode configurationXml, string factoryName) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (configurationXml == null) { throw new ArgumentNullException("configurationXml"); } try { XmlDocument configurationDocument = new XmlDocument(); configurationDocument.LoadXml(configurationXml.OuterXml); // Add Proper Namespace var namespaceManager = new XmlNamespaceManager(configurationDocument.NameTable); namespaceManager.AddNamespace(NamespacePrefix, SchemaNamespace); // Query Elements var nhibernateNode = configurationDocument.SelectSingleNode(RootXPath + SectionName, namespaceManager); if (nhibernateNode == null) { throw new ConfigurationErrorsException(string.Format(CultureInfo.InvariantCulture, "<{0} xmlns=\"{1}\"> element was not found in the configuration file.", SectionName, SchemaNamespace)); } if (nhibernateNode.SelectSingleNode(RootXPath + "session-factory[@name='" + factoryName + "']", namespaceManager) == null) { throw new ConfigurationErrorsException(string.Format(CultureInfo.InvariantCulture, "<session-factory name=\"{0}\"> element was not found in the configuration file.", factoryName)); } foreach (XmlNode node in nhibernateNode.SelectNodes(RootXPath + "session-factory[@name!='" + factoryName + "']", namespaceManager)) { nhibernateNode.RemoveChild(node); } using (var textReader = new XmlTextReader(new StringReader(nhibernateNode.OuterXml))) { return(configuration.Configure(textReader)); } } catch { throw; } }
private static ISessionFactory CreateSessionFactory() { Logger.DebugFormat("DataDirectory: {0}", AppDomain.CurrentDomain.GetData("DataDirectory")); // delete the existing db on each run string dbname = ConfigurationManager.AppSettings["Database.Filename"]; Logger.DebugFormat("Try delete: {0}", dbname); if (dbname != null && File.Exists(dbname)) File.Delete(dbname); var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(); return Fluently.Configure(cfg) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProjectMapping>()) .ExposeConfiguration(BuildSchema) .BuildSessionFactory(); }
private static void Configure() { _config.Configure(XmlReader.Create(new StringReader( "<hibernate-configuration xmlns=\"urn:nhibernate-configuration-2.2\">" + "<session-factory>" + "<property name=\"cache.provider_class\">NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2</property>" + "<property name=\"cache.use_query_cache\">true</property>" + "<property name=\"connection.provider\">NHibernate.Connection.DriverConnectionProvider</property>" + "<property name=\"dialect\">NHibernate.Dialect.MsSql2008Dialect</property>" + "<property name=\"connection.driver_class\">NHibernate.Driver.SqlClientDriver</property>" + "<property name=\"show_sql\">false</property>" + "<property name=\"cache.use_second_level_cache\">true</property>" + "<property name=\"adonet.batch_size\">50</property>" + "</session-factory>" + "</hibernate-configuration>"))); string connectionString = ConfigurationManager.ConnectionStrings[new Ezbob.Context.Environment().Context.ToLower()].ConnectionString; _config.Properties.Add("connection.connection_string", connectionString); }
internal static ISessionFactory CreateConfigurationForConfigFileName(string configurationFile) { try { global::NHibernate.Cfg.Configuration cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(configurationFile); return cfg.BuildSessionFactory(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("NHibernate exception stack"); String formatter = "--"; do { System.Diagnostics.Debug.WriteLine(formatter + ex.Message); ex = ex.InnerException; formatter += "--"; } while (ex != null); throw; } }
internal static ISessionFactory CreateConfigurationForConfigFileName(string configurationFile) { try { global::NHibernate.Cfg.Configuration cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(configurationFile); return(cfg.BuildSessionFactory()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("NHibernate exception stack"); String formatter = "--"; do { System.Diagnostics.Debug.WriteLine(formatter + ex.Message); ex = ex.InnerException; formatter += "--"; } while (ex != null); throw; } }
private static ISessionFactory CreateSessionFactory() { Logger.DebugFormat("DataDirectory: {0}", AppDomain.CurrentDomain.GetData("DataDirectory")); // delete the existing db on each run string dbname = ConfigurationManager.AppSettings["Database.Filename"]; Logger.DebugFormat("Try delete: {0}", dbname); if (dbname != null && File.Exists(dbname)) { File.Delete(dbname); } var cfg = new global::NHibernate.Cfg.Configuration(); cfg.Configure(); return(Fluently.Configure(cfg) .Mappings(m => m.FluentMappings.AddFromAssemblyOf <ProjectMapping>()) .ExposeConfiguration(BuildSchema) .BuildSessionFactory()); }
private static NhibConfigData GetOrCreateConfigData(String configFileName) { NhibConfigData retvalue = factories.SafeGet(configFileName); if (null == retvalue) { //This is the first time we ask for this configuration global::NHibernate.Cfg.Configuration config = new global::NHibernate.Cfg.Configuration(); XDocument doc = XDocument.Load(configFileName); XElement connStringElement = (from e in doc.Descendants() where e.Attribute("name") != null && e.Attribute("name").Value == "connection.connection_string" select e).Single(); String cnName = connStringElement.Value; connStringElement.Value = ConfigurationRegistry.ConnectionString(connStringElement.Value).ConnectionString; using (XmlReader reader = doc.CreateReader()) { config.Configure(reader); } ISessionFactory factory = config.BuildSessionFactory(); retvalue = new NhibConfigData() { Configuration = config, SessionFactory = factory, ConnectionName = cnName }; factories.Add(configFileName, retvalue); } return retvalue; }