簡體   English   中英

流利的Nhibernate和SQLite映射組件

[英]Fluent Nhibernate & SQLite mapping component

我在實體上有一個組件,可能為null。 這適用於InMemory數據庫的單元測試,但不適用於基於文件的SQLite數據庫。 我使用布爾值標志來指示是否設置了組件,但這似乎是一個hack。

這是NHibernate還是SQLite錯誤? 還是我錯過了什么?

這是我失去業務價值的映射:

public sealed class EntityMap : ClassMap<Entity>
{
   public EntityMap()
   {
     Not.LazyLoad();
     Id(m => m.Uid);
     Map(m => m.HasComponent).Not.Nullable();
     Component(m => m.Component).ColumnPrefix("Component");
   }
}

public sealed class MyComponentMap : ComponentMap<MyComponent>
{
   public MyComponentMap()
   {
     Map(c => c.SomeBasicProperty).Nullable();
     HasMany(c => c.ListOfValues).AsList(li => li.Column("Number"))
                           .Component(part => 
                                         {
                                             part.Map(s => s.Name);
                                             part.Map(s => s.Value);
                                         }
                            .Table("ComponentValues")
                            .Cascade.AllDeleteOrphan().Not.LazyLoad();

   }
}

對於內存數據庫,當實體上的所有列都為空時,實體上的組件為空。 這就是我的期望。 使用基於文件的數據庫時,該組件為空。 所以我不能使用entity.Component==null來檢查是否已設置組件。

我用內存測試了您的代碼,並且按預期方式為內存數據庫返回Component != null 在NHibernate中,集合永遠不會為空,而可以為空,並且如果所有屬性都為null,則組件只能為null,因此組件永遠不能為null。 我想你測試得像

using (var tx = session.BeginTransaction())
{
    id = session.Save(new Entity());
    tx.Commit();
}

var entity = session.Get<Entity>(id);
Console.WriteLine(entity.Component == null);

Get返回將Component設置為null的同一實例。

using (var tx = session.BeginTransaction())
{
    id = session.Save(new Entity());
    tx.Commit();
}

session.Clear();

var entity = session.Get<Entity>(id);
Console.WriteLine(entity.Component == null);

Get返回已加載的實例,該實例的Component設置為非null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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