简体   繁体   English

流利的Nhibernate和SQLite映射组件

[英]Fluent Nhibernate & SQLite mapping component

I have a component on an entity that may be null. 我在实体上有一个组件,可能为null。 This works in a unit test with an InMemory database, but does not work on a file based SQLite database. 这适用于InMemory数据库的单元测试,但不适用于基于文件的SQLite数据库。 I use a boolean flag to indicate whether the component was set, but this seems to be a hack. 我使用布尔值标志来指示是否设置了组件,但这似乎是一个hack。

Is this a NHibernate or a SQLite Bug? 这是NHibernate还是SQLite错误? Or am i missing something? 还是我错过了什么?

Here are my mappings stripped of business value: 这是我失去业务价值的映射:

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();

   }
}

With the memory database the component on the entity is null when all columns for it are null. 对于内存数据库,当实体上的所有列都为空时,实体上的组件为空。 This is what i expect. 这就是我的期望。 When using a file based database the component is empty. 使用基于文件的数据库时,该组件为空。 So i cannot use entity.Component==null to check if the Component has already been set. 所以我不能使用entity.Component==null来检查是否已设置组件。

i tested your code with inmemory and it returns Component != null for an inmemory database as expected. 我用内存测试了您的代码,并且按预期方式为内存数据库返回Component != null In NHibernate a collection is never null but can be empty and a component is only null if all properties are null hence the component should never be null. 在NHibernate中,集合永远不会为空,而可以为空,并且如果所有属性都为null,则组件只能为null,因此组件永远不能为null。 i guess that you tested like 我想你测试得像

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

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

Get returns the same instance which has Component set to 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 returns the loaded instance which has Component set to non-null. Get返回已加载的实例,该实例的Component设置为非null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM