-2

NHibernate create database Automatically for SQLite using

new SchemaUpdate(configuration).Execute(false, true);

When I try with MySQL ,I see that first I have to create a schema manually, then NHibernate create tables automatically for me.

To give more concrete example Suppose that I have a connection string

<property name="connection.connection_string">Database=mydatabase;
 Data Source=localhost;User Id=x;Password=y</property> 

If I do not create a new schema which has same name as Database in connection string manuelly or programatically like this :

  "CREATE SCHEMA IF NOT EXISTS " + "mydatabase";

NHibernate gives me error : Unknown Database "mydatabase".

If There is a schema called "mydatabase" exist before, NHibernate create tables automatically

So here is my short question :

Is there a way for MYSQL database which NHibernate create database automatically without creating schema before in MySQL manually? If so how?

Hippias Minor
  • 1,917
  • 2
  • 21
  • 46
  • 1
    Have you seen this question http://stackoverflow.com/questions/914371/is-it-possible-to-create-a-database-using-nhibernate – Rippo Jun 18 '14 at 13:19
  • Yes. I see. But NHibernate Create Database For SQLite. Are you sure it does not create for MySQL? – Hippias Minor Jun 18 '14 at 13:23
  • And Entity Framework Create Database also for MYSQL automatically. Why not NHibernate? – Hippias Minor Jun 18 '14 at 13:23
  • Did you read the answers, sqlite needs no special config, whereas mssql and mysql needs more than just `CREATE SCHEMA foo;` – Rippo Jun 18 '14 at 13:26
  • So, I look for answer. It is not possible to create database automatically in MySQL or not? – Hippias Minor Jun 18 '14 at 13:28
  • Not unless you look at the accepted answer and adapt it to your needs.... – Rippo Jun 18 '14 at 13:43
  • Unless ?...Rippo this is a techical question with simple YES NO...Is it possible to create database aotumatically in MySQL with NHibernate?I do not ask you how to make a watch...Just ask you what time is it...So simple – Hippias Minor Jun 18 '14 at 13:53
  • Not all technical questions have absolute yes or no answers. Actually read and think about the excellent link @Rippo gave you. – simon at rcl Jun 18 '14 at 15:01
  • YES it is possible although you need to hack some code... Read accepted answer in link. HTH – Rippo Jun 18 '14 at 15:16
  • There is no MySQL sample there...And all the things that Ayende guy do is create tables using ADO NET[Raw SQL ]. Does not related to NHibiernate automatic database creation. – Hippias Minor Jun 18 '14 at 15:47
  • Simon, I think you work today to hard.Relax. Not all but the question "Can we create database automatically or not With NHibernate in MySQL ?" must have concrete YES or No Answer. – Hippias Minor Jun 18 '14 at 15:50
  • This is my last comment, I shall repeat the comment `Not unless you look at the accepted answer and adapt it to your needs.` Look at this link https://svn.code.sf.net/p/rhino-tools/code/trunk/commons/Rhino.Commons.NHibernate/ForTesting/UnitOfWorkTestContextDbStrategy.cs and look at the method `CreateDatabaseMedia`. He is not just creating tables! – Rippo Jun 18 '14 at 16:58
  • 1
    Down-voted for obstinacy. – Daniel Schilling Jun 18 '14 at 18:40
  • Yet Another Dimension of Idiocy Schilling ... Stackoverflow has to stop those child-ish behaviours or it will be lose its light. – Hippias Minor Jun 19 '14 at 07:02
  • And Rippo... You do not have to answer anything. But if you want to answer, answer it properly... I am bored from those childish behaviours. I am not your nanny...Grow up guys – Hippias Minor Jun 19 '14 at 07:03
  • Rippo ... You are really funny ... Look CreateDatabaseMedia? He just create tables and use new SchemaExport(TestContext.Configuration).Execute(false, true, false); for SQLite... I said this does not work in MYSQL. Did you not read my question? – Hippias Minor Jun 19 '14 at 07:09
  • @HippiasMinor Be sure to notice the difference between CreateDatabaseSchema() and CreateDatabaseMedia(), the latter of which is abstract in the base class and implemented in several subclasses. Some of which include the literal text "... CREATE DATABASE ...". – Oskar Berggren Jun 19 '14 at 10:12
  • Ah yes @HippiasMinor be sure to scroll down a bit to the sub classes bit :) – Rippo Jun 20 '14 at 09:42
  • You are funny and Guys At NHibernate also funny.... https://nhibernate.jira.com/browse/NH-3621 :-) – Hippias Minor Jun 20 '14 at 11:51

1 Answers1

1

NHibernate does not create databases for any database system. SQLite itself however will create any database that you ask for if it doesn't already exist, at least when used through the System.Data.SQLite.dll. This is different from most other database engines.

The fact that some action is not needed for one database engine does not in itself imply that NHibernate must promise to do it for you for any other database engine.

For the question:

Is it possible to create database automatically in MySQL?

The answer is: "Yes, just write the code to perform the required SQL statements."

For the question:

Will NHibernate create a MySQL database automatically?

The answer is: "No".

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
  • Thanks for clear and brief answer. But EF from Microsoft create Database for MYSQL [ interestingly it does not do for SQLite ]. For me if I write required SQL statements why bother with ORM framework.And NHibernate should definelly support this feature. – Hippias Minor Jun 23 '14 at 07:45
  • creating a database has additional parameter which *are important*, in MySql it is `CHARACTER SET` and `COLLATE` (other databases have something similar) which are not part of the connectionstring, dialect or driver. How would NHibernate pick the right one? Also it's surprising that a spelling error in the connection string silently creates a new database instead of telling you – Firo Jan 19 '15 at 14:08
  • @HippiasMinor This is old now, but I should clarify since often ambivalent terms are used. NHibernate will not create the database, however, if you point it to an existing (but empty) database, it can create the schema definitions of tables etc inside that database. It doesn't happen automatically, thankfully. You would have to make the required calls to NHibernate to trigger it. – Oskar Berggren Jan 16 '18 at 16:50