如何用NHibernate删除所有数据库数据?
是否可以使用NHibernate删除数据库中的所有数据。我想在开始每个单元测试之前这样做。目前,我删除数据库并再次创建它,但这对我来说不是可接受的解决方案。
=================================================== ========
好的,这是结果。我正在数据库(Postgre)上对此进行测试。我将测试 CreateSchema(1)、DanP 解决方案(2) 和 apollodude217 解决方案(3)。我使用每种方法运行测试 5 次,并取平均时间。
第 1 轮 - 10 次测试
(1) - ~26 秒
(2) - 9,0 秒
(3) - 9,3 秒
第 2 轮 - 100 次测试
(1) - 拜托,我不会在我的机器上这样做
(2) - 12,6 秒
(3) - 18,6 秒
我认为没有必要进行更多测试。
Is it possible to delete all data in the database using NHibernate. I want to do that before starting each unit test. Currently I drop my database and create it again but this is not acceptable solution for me.
==========================================================
Ok, here are the results. I am testing this on a database (Postgre). I will test CreateSchema(1), DanP solution(2) and apollodude217 solution(3). I run the tests 5 times with each method and take the average time.
Round 1 - 10 tests
(1) - ~26 sec
(2) - 9,0 sec
(3) - 9,3 sec
Round 2 - 100 tests
(1) - Come on, I will not do that on my machine
(2) - 12,6 sec
(3) - 18,6 sec
I think that it is not necessary to test with more tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我正在使用
SchemaExport
类并在每次测试之前重新创建架构。这几乎就像删除数据库一样,但它只是删除并重新创建表。我认为从每个表中删除所有数据并不比这更快,甚至可能更慢。我们的单元测试通常在内存中的Sqlite上运行,这是非常快的。该数据库仅在连接打开时存在,因此每次测试都会重新创建整个数据库。我们通过更改构建配置切换到 Sqlserver。
I'm using the
SchemaExport
class and recreate the schema before each test. This is almost like dropping the database, but it's only dropping and recreating the tables. I assume that deleting all data from each table is not faster then this, it could even be slower.Our unit tests are usually running on Sqlite in memory, this is very fast. This database exists only as long as the connection is open, so the whole database is recreated for each test. We're switching to Sqlserver by changing the build configuration.
就我个人而言,我使用存储过程来执行此操作,但使用可执行 HQL 可能是可能的(有关更多详细信息,请参阅此帖子:http://fabiomaulo.blogspot.com/2009/05/nh21-executable-hql.html )
类似于 session.Delete("from object" );
Personally, I use a stored procedure to do this, but it may be possible with 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");
我并不声称这更快,但您可以对每个映射类执行类似的操作:
这(单独)在至少两种情况下不起作用:
I do not claim this is faster, but you can do something like this for each mapped class:
This (alone) will not work in at least 2 cases:
一个好的替代方案是备份初始数据库状态并在开始测试时恢复它(这可能很复杂也可能不复杂,具体取决于数据库)
A good alternative is having a backup of the initial DB state and restoring it when starting tests (this can be complex or not, depending on the DB)
重新创建数据库是一个不错的选择,特别是对于单元测试。如果创建脚本太慢,您可以在每次测试之前备份数据库并使用它将数据库恢复到初始状态。
另一种方法是编写一个脚本,删除数据库中的所有外键,然后删除/截断所有表。然而,这不会重置任何自动生成的 ID 或序列。这看起来不是一个优雅的解决方案,而且肯定更耗时。
无论如何,这不应该通过 ORM 来完成,而不仅仅是 NHibernate。
您为什么拒绝重新创建选项?您有什么要求?模式是否太复杂?数据库是由其他人设计的吗?您想避免文件碎片吗?
Re-creating the database is a good choice, especially for unit testing. If the creation script is too slow you could take a backup of the database and use it to restore the DB to an initial state before each test.
The alternative would be to write a script that would drop all foreign keys in the database then delete/truncate all tables. This would not reset any autogenerated IDs or sequences however. This doesn't seem like an elegant solution and it is definitely more time consuming.
In any case, this is not something that should be done through an ORM, not just NHibernate.
Why do you reject the re-creation option? What are your requirements? Is the schema too complex? Does someone else design the database? Do you want to avoid file fragmentation?
另一种解决方案可能是创建一个擦除数据的存储过程。在测试设置或实例化方法中,首先运行存储过程。
但是我不确定这是否比任何其他方法更快,因为我们不知道数据库的大小和可能删除的行数。另外,出于安全目的,我不建议将此存储过程部署到实时服务器!
华泰
Another solution might be to create a stored procedure that wipes the data. In your test set up or instantiate method run the stored procedure first.
However I am not sure if this is quicker than any of the other methods as we don't know the size of database and number of rows likely to be deleted. Also I would not recommend deploying this stored procedure to the live server for safety purposes!
HTH