Exemplo n.º 1
0
        /// <summary>
        /// Execute schema update script, determined by the Configuration object
        /// used for creating the SessionFactory. A replacement for NHibernate's
        /// SchemaUpdate class, for automatically executing schema update scripts
        /// on application startup. Can also be invoked manually.
        /// </summary>
        /// <remarks>
        /// Fetch the LocalSessionFactoryObject itself rather than the exposed
        /// SessionFactory to be able to invoke this method, e.g. via
        /// <code>LocalSessionFactoryObject lsfo = (LocalSessionFactoryObject) ctx.GetObject("mySessionFactory");</code>.
        /// <p>
        /// Uses the SessionFactory that this bean generates for accessing a ADO.NET
        /// connection to perform the script.
        /// </p>
        /// </remarks>
        public virtual void UpdateDatabaseSchema()
        {
            log.Info("Updating database schema for Hibernate SessionFactory");
            HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);

            hibernateTemplate.TemplateFlushMode = TemplateFlushMode.Never;
            hibernateTemplate.Execute(
                new HibernateDelegate(delegate(ISession session)
            {
                IDbConnection con         = session.Connection;
                Dialect dialect           = Dialect.GetDialect(Configuration.Properties);
                DatabaseMetadata metadata = new DatabaseMetadata((DbConnection)con, dialect);
                string[] sql = Configuration.GenerateSchemaUpdateScript(dialect, metadata);
                ExecuteSchemaScript(con, sql);
                return(null);
            }));
        }
		public void Contribute(string alias, ISessionFactory factory, Configuration cfg)
		{
			Configuration = cfg;
			SessionFactory = factory;
			if (CreateDatabaseSchema)
			{
				using (var session = factory.OpenSession())
				{
					var dialect = Dialect.GetDialect(cfg.Properties);

					var metadata = new DatabaseMetadata((DbConnection) session.Connection, dialect);

					string[] lines = cfg.GenerateSchemaUpdateScript(dialect, metadata);

					foreach (var line in lines)
					{
						var cmd = session.Connection.CreateCommand();
						cmd.CommandText = line;
						cmd.ExecuteNonQuery();
					}
				}

				DatabaseSchemaCreated = true;
			}
		}
 public static string[] ExportUpdateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)
 {
     Configuration cfg = new Configuration();
     cfg.Configure();
     return cfg.GenerateSchemaUpdateScript(dialect, databaseMetadata);
 }