Reputation: 618
I am new in both NHibernate and Fluent NHibernate. What I would like to achieve is to choosing the appropriate database provider and click on a button, to generate database if not exists and create all tables, relations, etc.
At the beginning of described above I met a problem. Namely, the following code is not creating a database - as a return I am receiving an error. The same situation is both for NHibernate with xml, and Fluent NHibernate. If the database is created by me manually, the code is working properly, the table is created. But if it does not exist, not.
Here is NHibernate and xml approach:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.Sql2012ClientDriver</property>
<property name="connection.connection_string">Server=.\SQLEXPRESS;Database=databaseName;Initial Catalog=databaseName;User id=user;password=xxx</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
And here is Fluent NHibernate approach:
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(@"Server=.\SQLEXPRESS;Database=databaseName;Initial Catalog=databaseName;User id=user;password=xxx")
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entry>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
All commented approaches (SchemaUpdate and adding property) did not give any positive result. Still I am receiving the following error:
Cannot open database "databaseName" requested by the login. The login failed.Login failed for user 'user'.
I have tried this both for MSSQL Express 2012 and localdb in Visual Studio 2015. I am aware that finally I should use SchemaUpdate for not loosing data, but for development I could use any of the tried approach.
I wish to have a possibility to support in this solution the following databases: MSSQL, Oracle, MySQL, PostgreSQL.
Please advice what I am doing wrong. Thanks.
Upvotes: 0
Views: 1395
Reputation: 64628
SchemaExport doesn't create the database. It only creates tables and other database objects.
Creating databases is usually very configuration intensive and requires more permissions. These may have been the reason why it was never implemented as part of SchemaExport.
Upvotes: 1