/// <summary> /// The create database. /// </summary> public void CreateSchema() { if (Configuration == null) { return; } var export = new SchemaExport(Configuration); #if DEBUG CreateLogDir(); using (var file = new FileStream( @"C:\TestOutput\NHibernate\CreateSchema.sql", FileMode.Create, FileAccess.Write)) { using (var sw = new StreamWriter(file)) { export.Execute(true, true, false, CurrentSession?.Connection, sw); } } #else export.Execute(true, true, false, this.CurrentSession?.Connection, null); #endif }
public void BuildSchema(DbConnection connection = null) { var path = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, $@"schema{connection?.DataSource}.sql"); // this NHibernate tool takes a configuration (with mapping info in) // and exports a database schema from it var schemaExport = new SchemaExport(this._configuration); schemaExport.SetOutputFile(path); schemaExport.Create( useStdOut: true, execute: false); if (connection != null) { schemaExport.Execute( useStdOut: false, execute: true, justDrop: false, connection: connection, exportOutput: null); } else { schemaExport.Execute( useStdOut: false, execute: true, justDrop: false); } }
public string Create() { var schemaExport = new SchemaExport(configuration); databaseProvider.CreateIfNotExists(); var stringBuilder = new StringBuilder(); schemaExport.Create(x => stringBuilder.Append(x), false); var statement = stringBuilder.ToString(); statement = string.IsNullOrWhiteSpace(statement) ? null : statement; if (!databaseProvider.Exists()) { databaseProvider.Create(); schemaExport.Execute(false, true, false); } else { try { new SchemaValidator(configuration).Validate(); } catch { schemaExport.Execute(false, true, false); } } return(statement); }
// Drop old database if any, create new schema private void RecreateSchema(NHibernate.Cfg.Configuration cfg, string connectionStringName) { #if !DEBUG return; #endif bool writeOutput = false; RunSql(connectionStringName, connection => { // NH schema export does not correctly drop all constraints // SQL from http://stackoverflow.com/a/26348027 new SqlCommand(@" exec sp_MSforeachtable ""declare @name nvarchar(max); set @name = parsename('?', 1); exec sp_MSdropconstraints @name""; " , connection).ExecuteNonQuery(); var export = new SchemaExport(cfg); if (writeOutput) { using (var writer = new StreamWriter(@"C:\Temp\vdb.sql")) { export.Execute(false, true, false, connection, writer); } } else { export.Execute(false, true, false, connection, null); } }); }
private static void ExportSchema(Configuration configuration) { var schemaExport = new SchemaExport(configuration); schemaExport.SetOutputFile(SchemaOutputPath); schemaExport.Execute(false, false, false); }
/// <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); } }
public static void CreateDatabase() { Configuration config = CreateNHibernateConfiguration().BuildConfiguration(); var exporter = new SchemaExport(config); exporter.Execute(true, true, false); }
public void TestCleanup() { //Reset database data after every run SchemaExport exp = new SchemaExport(_cfg); exp.Execute(true, true, false); }
public static IAuthenticationService AuthenticationService() { if (configured == false) { string applicationName = "sops"; IPersistenceConfigurer persistenceConfigurer = MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("DefaultConnection")); service = new FormsAuthenticationService(applicationName, new BCryptStrategy(), new ConsoleLogger(), persistenceConfigurer, configuration => { var se = new SchemaExport(configuration); se.Drop(true, true); se.Execute(true, true, true); var su = new SchemaUpdate(configuration); su.Execute(true, true); }); service.Configure(); configured = true; } return(service); }
public void Script() { var automapper = new AutoMapper { EntityBaseType = typeof(AutoMapperTestEntityBase) }; automapper.MapAssemblyOf <SchemaTests>(); var compiled = automapper.Complete(); var configuration = new Configuration(); configuration.DataBaseIntegration(db => { db.Dialect <SQLiteDialect>(); db.ConnectionString = "Data Source=:memory:;Version=3;New=True"; }); configuration.AddDeserializedMapping(compiled.First(), string.Empty); var export = new SchemaExport(configuration); export.Execute(true, false, false); }
public void TestInitizlize() { var config = new Configuration(); config.DataBaseIntegration( db => { db.Dialect <SQLiteDialect>(); db.Driver <SQLite20Driver>(); db.ConnectionString = ConnectionString; db.LogSqlInConsole = false; db.LogFormattedSql = true; db.AutoCommentSql = true; } ); config.SetProperty(Environment.CurrentSessionContextClass, "thread_static"); var mapper = new ModelMapper(); mapper.WithMappings(config); ContextSessionFactory = config.BuildSessionFactory(); ContextSession = ContextSessionFactory.OpenSession(); var schemaExport = new SchemaExport(config); schemaExport.Execute(false, true, false, ContextSession.Connection, TextWriter.Null); Context(); BecauseOf(); }
void SetupQueryIntegrationTest(ISession session) { using (var tran = session.BeginTransaction()) { var exporter = new SchemaExport(config); exporter.Execute(false, true, false, session.Connection, null); tran.Commit(); } using (var tran = session.BeginTransaction()) { var pet = new Pet { Identity = 1, Owner = new Person { Identity = 1, Name = "Jane Doe" } }; session.Save(pet); session.Save(pet.Owner); session.Flush(); tran.Commit(); } session.Clear(); }
public static ISessionFactory CreateSessionGeral() { var _session = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(connectionGeral)) .Mappings(m => m.FluentMappings .Add <UsuarioMap>() .Add <EnderecoMap>() .Add <EstacaoService>() .Add <VeiculoMap>() .Add <EmpresaMap>() ); var session = _session.BuildConfiguration(); if (!initGeral) { var schemaExport = new SchemaExport(session); schemaExport.Execute(false, true, true); schemaExport.Drop(false, true); schemaExport.Create(false, true); var updater = new SchemaUpdate(session); updater.Execute(true, true); var ex = updater.Exceptions; initGeral = true; } return(_session.BuildSessionFactory()); }
public void Throwing_IsIntercepted_InMediumTrust() { IEngine engine = new ContentEngine(new MediumTrustServiceContainer(), new EventBroker(), new ContainerConfigurer()); engine.Initialize(); var schemaCreator = new SchemaExport(engine.Resolve <IConfigurationBuilder>().BuildConfiguration()); var conn = engine.Resolve <ISessionProvider>().OpenSession.Session.Connection; schemaCreator.Execute(false, true, false, conn, null); engine.SecurityManager.Enabled = false; ContentItem root = new ThrowableItem(); root.Name = "root_mediumtrust"; ContentItem item = new ThrowableItem(); item.Name = "bin's destiny"; item.AddTo(root); engine.Persister.Save(root); engine.Resolve <IHost>().DefaultSite.RootItemID = root.ID; engine.Resolve <IHost>().DefaultSite.StartPageID = root.ID; engine.Persister.Delete(item); Assert.That(root.Children.Count, Is.EqualTo(1)); Assert.That(root.Children[0], Is.TypeOf(typeof(TrashContainerItem))); Assert.That(root.Children[0].Children[0], Is.EqualTo(item)); }
protected static void ResetDB() { Configuration config = GetNHibernateConfig(); var schemaExport = new SchemaExport(config); schemaExport.Execute(true, true, false); }
public static ISessionFactory CreateSessionPacote() { var _session = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(connectionPacote)) .Mappings(m => m.FluentMappings .Add <PacoteMap>() .Add <RotaMap>() .Add <LocalizacaoMap>() ); var session = _session.BuildConfiguration(); if (!initPacote) { var schemaExport = new SchemaExport(session); schemaExport.Execute(false, true, true); schemaExport.Drop(false, true); schemaExport.Create(false, true); var updater = new SchemaUpdate(session); updater.Execute(true, true); var ex = updater.Exceptions; initPacote = true; } return(_session.BuildSessionFactory()); }
private static void BuildSchema(NHibernate.Cfg.Configuration config) { var export = new SchemaExport(config); export.SetOutputFile(@"c:\output.sql"); export.Execute(true, false, false); }
public void CanCreate_FromScratch() { SchemaExport schemaCreator = new SchemaExport(CreateConfiguration(xml1)); schemaCreator.Execute((s) => { Debug.WriteLine(" === Create === "); Debug.WriteLine(s); }, true, false); schemaCreator.Execute((s) => { Debug.WriteLine(" === Drop === "); Debug.WriteLine(s); }, true, true); }
public void ShouldExportSchema() { schemaExport.SetOutputFile("../../../sqloutput/ddl.sql"); schemaExport.Execute(false, true, false); dropDatabase = true; }
public static bool RebuildSchema(string dud = null) { var configuration = new Configuration(); configuration.AddAssembly(typeof(Newsgroup).Assembly); configuration.Configure(); using (var connection = new SQLiteConnection(configuration.GetProperty("connection.connection_string"))) { connection.Open(); try { var export = new SchemaExport(configuration); export.Execute(false, true, false, connection, null); WriteBaselineData(); } catch (Exception ex) { _Logger.Error("Unable to rebuild the database schema", ex); throw; } finally { connection.Close(); } } return(false); }
private static Task BugAsync(Configuration cfg, CancellationToken cancellationToken = default(CancellationToken)) { try { var su = new SchemaExport(cfg); var sb = new StringBuilder(500); su.Execute(x => sb.AppendLine(x), false, false); string script = sb.ToString(); if (Dialect.Dialect.GetDialect(cfg.Properties).SupportsIfExistsBeforeTableName) { Assert.That(script, Does.Match("drop table if exists nhibernate.dbo.Aclass")); } else { Assert.That(script, Does.Match("drop table nhibernate.dbo.Aclass")); } Assert.That(script, Does.Match("create table nhibernate.dbo.Aclass")); return(Task.CompletedTask); } catch (System.Exception ex) { return(Task.FromException <object>(ex)); } }
private void EnsureMasterDatabaseExistence() { if (configuration == null) { lock (masterDatabaseSync) { if (configuration == null) { string connectionString = BuildConnectionString(masterDatabaseFileName); if (File.Exists(masterDatabaseFileName)) { File.Delete(masterDatabaseFileName); } using (var engine = new SqlCeEngine(connectionString)) { engine.CreateDatabase(); } configuration = BuildConfigurationInternal(masterDatabaseFileName); sessionFactory = configuration.BuildSessionFactory(); using (var masterConnection = new SqlCeConnection(connectionString)) { masterConnection.Open(); SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.Execute(false, true, false, masterConnection, TextWriter.Null); } } } } }
private static Configuration Configure() { var config = Fluently.Configure() .Database( SQLiteConfiguration.Standard.UsingFile("database").ShowSql() //MsSqlConfiguration.MsSql2008.ConnectionString(b => b.FromConnectionStringWithKey("db")) ) .Mappings(m => { var model = AutoMap.Assemblies(new AutomappingConfiguration(), GetAssemblies()); model.Conventions.Add(new SetEnumTypeConvention()); model.Conventions.Add(new UseNewSqlDateTime2TypeConvention()); model.Conventions.Add(new CollectionAccessConvention()); model.Conventions.Add(new SqlTimestampConvention()); model.Conventions.Add(new SetTableNameConvention()); model.Conventions.Add(DefaultLazy.Never()); m.AutoMappings.Add(model); m.AutoMappings.ExportTo("c:\\mapping"); m.FluentMappings.ExportTo("c:\\mapping"); }) .BuildConfiguration(); // Generatre schema before each start var e = new SchemaExport(config); e.SetOutputFile(@"c:\mapping\a.sql"); e.Execute(true, true, false); return(config); }
static ISessionFactory CreateSessionFactoryForDdl() { FluentConfiguration cfg = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(c => c.FromConnectionStringWithKey("MSSQL2012")) .Dialect <MsSql2012Dialect>() ); cfg.Mappings(m => m .FluentMappings .AddFromAssemblyOf <NHibernateHelper>() ); cfg.ExposeConfiguration(config => { SchemaExport schemaExport = new SchemaExport(config); schemaExport.SetOutputFile(AppDomain.CurrentDomain.BaseDirectory + @"\LedgerLinkDbScript.sql"); schemaExport.Drop(true, true); schemaExport.Create(true, true); schemaExport.Execute(false, false, false); }); SessionFactory = cfg.BuildSessionFactory(); return(SessionFactory); }
public void SetupContext() { _logger.Debug("Setting up context"); var export = new SchemaExport(_cfg); export.Execute(false, true, false, false); }
public static void Setup(out ContentPersister persister, ISessionProvider sessionProvider, N2.Persistence.IRepository <ContentItem> itemRepository, INHRepository <ContentDetail> linkRepository, SchemaExport schemaCreator) { persister = new ContentPersister(itemRepository, linkRepository); new BehaviorInvoker(persister, new N2.Definitions.Static.DefinitionMap()).Start(); schemaCreator.Execute(false, true, false, sessionProvider.OpenSession.Session.Connection, null); }
public void Setup() { // Create NHibernate DB tables _schemaExport.Execute(false, true, false); SetupTestData(); }
public void CreateDatabaseSchema() { var export = new SchemaExport( DataContext.BuildConfiguration()); export.Execute(true, true, false); }
protected void ReBuildDatabase() { var export = new SchemaExport(helper.Configuration); export.Execute(true, true, false); //var query = Session.CreateSQLQuery(hilo); //query.ExecuteUpdate(); }
public void CreateDB() { SchemaExport schemaExport = new SchemaExport(Common.GetNHibernateConnnectInfo()); schemaExport.Execute(false, true, false); CreateBlog(); }
public void SetUp() { cfg = new Configuration(); cfg.Properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider"; cfg.Properties["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver"; cfg.Properties["connection.connection_string"] = "Data Source=.\\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI"; cfg.Properties["dialect"] = "NHibernate.Dialect.MsSql2000Dialect"; cfg.Properties["proxyfactory.factory_class"] = "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"; cfg.Properties["show_sql"] = "true"; cfg.AddAssembly("BlogSharp.NHibernate"); factory = cfg.BuildSessionFactory(); var export = new SchemaExport(cfg); export.Execute( true, false, true); }
public void SetUp() { cfg=Fluently.Configure() .Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<UserMap>()) .Database(MsSqlConfiguration.MsSql2008 .AdoNetBatchSize(20) .ConnectionString(stringBuilder=> stringBuilder.Database("test") .TrustedConnection() .Server(".")) .ShowSql() .Raw("hbm2ddl.keywords", "auto-quote") ).BuildConfiguration(); factory = cfg.BuildSessionFactory(); var export = new SchemaExport(cfg); export.Execute(true, true, false); }
/// <summary> /// Gets an NHibernate session for an in-memory database. /// </summary> /// <returns> /// A session for an in-memory database. /// </returns> private static ISession GetInMemoryDatabaseSession() { Configuration configuration = null; ISessionFactory sessionFactory = Fluently.Configure() .Database( SQLiteConfiguration.Standard .InMemory() .ShowSql()) .Mappings( m => m.FluentMappings.AddFromAssemblyOf<StockMap>()) .ExposeConfiguration( cfg => configuration = cfg) .BuildSessionFactory(); var session = sessionFactory.OpenSession(); var schemaExport = new SchemaExport(configuration); schemaExport.Execute(true, true, false, session.Connection, null); return session; }
public void TearDown() { factory = cfg.BuildSessionFactory(); var export = new SchemaExport(cfg); export.Execute(true, false, true); }