NHibernate 不删除实体
在 NUnit 测试的 TestFixtureTearDown 部分中,我尝试删除在 TestFixtureSetUp 部分中创建的一些测试实体。我使用以下代码
sessionFactory = NHibernateHelper.CreateSessionFactory(cssc["DefaultTestConnectionString"].ConnectionString);
uow = new NHibernateUnitOfWork(sessionFactory);
var g = reposGebruiker.GetByName(gebruiker.GebruikerNaam);
reposGebruiker.Delete(g);
var k = reposKlant.GetByName(klant.Naam);
reposKlant.Delete(k);
// Commit changes to persistant storage
uow.Commit();
但是,提交后,这两个实体仍在数据库中。搜索后我发现 此页面关于SO,所以我添加了:
uow.Session.Flush();
但是,实体仍然保留在数据库中。有谁知道这是为什么?
In the TestFixtureTearDown-part of an NUnit test I try to delete some test-entities created in the TestFixtureSetUp-part. I use the following code
sessionFactory = NHibernateHelper.CreateSessionFactory(cssc["DefaultTestConnectionString"].ConnectionString);
uow = new NHibernateUnitOfWork(sessionFactory);
var g = reposGebruiker.GetByName(gebruiker.GebruikerNaam);
reposGebruiker.Delete(g);
var k = reposKlant.GetByName(klant.Naam);
reposKlant.Delete(k);
// Commit changes to persistant storage
uow.Commit();
However, after the commit, the two entities were still in the database. After searching on I came across this page on SO and so I added:
uow.Session.Flush();
However, still the entities remain in the DB. Does anyone have an idea as to why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未使用过您正在使用的 UoW 类,但我的项目是在帮助程序中使用 ISession.BeginTransaction 和 ISession.Transaction.Commit 实现的,如下所示:
进而:
这应该有效。
我想提一下,这是一个例子,你需要做一些抽象。
I've never used the UoW class you're using, but my projects are implemented using ISession.BeginTransaction and ISession.Transaction.Commit in a helper like this:
And then:
This should work.
I want to mention that this is an example, and you'd want to make some abstractions.
存储库是如何创建的?为了成功删除,必须将对象加载到发出删除命令的同一 UoW (ISession) 中。
Delete
方法使对象成为非持久性对象并将其标记为删除。How are the repositories created? In for the delete to succeed, the objects must be loaded in the same UoW (ISession) in which the Delete command is issued. The
Delete
method makes the objects non-persistent and marks them for deletion.