![](/img/trans.png)
[英]Fluent NHibernate: SQLite in-memory database and GUID generated on insert
[英]NHibernate problems with deleting records from In-Memory SQLite database
我正在嘗試將精力集中在NHibernate中的會話管理上。 我不確定100%是否以正確的方式進行操作。 所以這是我的問題。 我正在對NHibernate存儲庫進行數據庫操作(添加,更新,刪除和加載)的單元測試。 我已經能夠進行添加,更新和加載的單元測試,但是刪除記錄給我帶來了很多麻煩。
這是我的源代碼要點:
現在,我認為問題出在CanDeleteDoctor()測試的代碼中:
[Test]
public void CanDeleteDoctor()
{
using (DoctorsRepository repo = new DoctorsRepository())
{
try
{
repo.BeginTransaction();
repo.Save(doctors[0]);
Assert.IsNotNull(repo.GetByName(doctors[0].Name));
repo.Delete(doctors[0]);
Assert.IsNull(repo.GetByName(doctors[0].Name));
}
catch (Exception ex)
{
repo.RollbackTransaction();
Assert.Fail(ex.Message + "\n" + ex.StackTrace);
}
}
}
每當我運行此測試時,都會收到一個異常消息:
Test 'PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor' failed:
Expected: null
But was: <PrototypeSLM.Entities.Doctors>
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.IsNull(Object anObject)
at PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor() in c:\Users\Craig_2\Documents\Visual Studio 2012\Projects\PrototypeSLM\PrototypeSLM.Tests\Tests\DoctorsRepositoryTest.cs:line 100
Tests\DoctorsRepositoryTest.cs(105,0): at PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor()
我感覺這與我如何在內存數據庫中進行會話管理有關? 我嘗試在DoctorsRepository的Delete方法中添加_session.Flush(),但尚未解決問題。 刷新會話后,我收到一個新異常,說“無法訪問已處置的對象”。
有什么提示可以幫助我解決這個問題嗎?
我通過更改CanDeleteDoctor()測試中的repo.Delete(x)方法中的參數來解決此問題:
repo.Delete(doctors[0]);
至
repo.Delete(repo.GetByName(doctors[0].Name));
我認為問題在於,Doctors [0]的ID與數據庫中Doctors記錄的ID屬性不匹配。 因此,存儲庫最終沒有刪除任何內容。 得出結論很難。
更改后,我的測試已100%通過。
編輯:如果有人知道這里實際發生的事情,我很想聽聽一個解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.