How to delete all database data using NHibernate?

Is it possible to delete all data in a database using NHibernate. I want to do this before the start of each unit test. I am currently deleting my database and creating it again, but this is not an acceptable solution for me.

==================================================== ========

Ok, here are the results. I am testing this in a database (Postgre). I will test the solution of CreateSchema (1), DanP (2) and apollodude217 (3). I run tests 5 times with each method and take average time.

Round 1 - 10 tests
(1) - ~ 26 sec
(2) - 9.0 sec
(3) - 9.3 s

Round 2 - 100 tests
(1) - Come on, I won’t do this on my machine
(2) - 12.6 sec
(3) - 18.6 s

I think there is no need to test with a lot of tests.

+3
source share
6 answers

Personally, I use a stored procedure for this, but it is possible using Executable HQL (see this post for more details: http://fabiomaulo.blogspot.com/2009/05/nh21-executable-hql.html )

Something along the lines of session.Delete ("from object");

+2
source

I use the class SchemaExportand recreate the circuit before each test. It is almost like dropping a database, but it is only dropping and re-creating tables. I assume that deleting all the data from each table is not accelerated, it can be even slower.

Sqlite , . , , . Sqlserver, .

+3

, , - :

// untested
var entities = MySession.CreateCriteria(typeof(MappedClass)).List<MappedClass>();
foreach(var entity in entities)
    MySession.Delete(entity);  // please optimize

() , , :

  • , .
  • , identity "any".
+2

( , )

+1

, . script , .

script, , / . reset . , , , . , , ORM, NHibernate.

? ? ? - ? ?

0
source

Another solution might be to create a stored procedure that erases data. In your test mode or instance creation method, first execute the stored procedure.

However, I'm not sure if this is faster than any other method, since we do not know the size of the database and the number of rows that can be deleted. Also, I would not recommend deploying this stored procedure on a real server for security reasons!

NTN

0
source

Source: https://habr.com/ru/post/1753657/


All Articles