Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear NHibernate database fast

Tags:

c#

nhibernate

I am using NHibernate for ORM, and everything works fine.

Now I started to write some unit-tests (using the DB, I do not want to put tooo much effort in abstracting this away, I know its not perfect, but it works..).

I need to be sure that the DB is completly empty for some tests. I can, of course, create the whole DB. But that seems to be overkill and I think it takes longer...

Is there a DELETE_ALL command which clears all tables, I can use in NHibernate?

Chris

EDIT: A short update, I decided to go the SQLite way, no problem to change this with NHibernate. There are some pitfalls, I am using this config, and it works. Otherwise you might get "table not found" errors, due to nHibernate closing the connection while in session, resulting in a "lost" database...

For your convinience: Copy and paste...

.Database(SQLiteConfiguration.Standard.ConnectionString("Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;")  
                       .Raw("connection.release_mode", "on_close"))
                        .Mappings(obj => obj.AutoMappings.Add(_config.APModel));
like image 328
Christian Ruppert Avatar asked Jun 22 '10 14:06

Christian Ruppert


1 Answers

Drop and recreate the database. You can use schemaexport:

var export = new SchemaExport(config);
export.Drop(false, true);
export.Create(true, true);

Sql lite in memory runs faster for tests than a "normal" database, but the disadvantage is that the sql-lite dialect can be different than the sql dialect in production.

like image 143
Paco Avatar answered Oct 08 '22 01:10

Paco