簡體   English   中英

NHibernate從內存中SQLite數據庫刪除記錄的問題

[英]NHibernate problems with deleting records from In-Memory SQLite database

我正在嘗試將精力集中在NHibernate中的會話管理上。 我不確定100%是否以正確的方式進行操作。 所以這是我的問題。 我正在對NHibernate存儲庫進行數據庫操作(添加,更新,刪除和加載)的單元測試。 我已經能夠進行添加,更新和加載的單元測試,但是刪除記錄給我帶來了很多麻煩。

這是我的源代碼要點:

NHibernate Helper用於數據庫配置和映射

醫生班級映射

用於數據庫操作的Doctors信息庫

Doctors Repository DB操作的單元測試

現在,我認為問題出在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM