流利的nhibernate与基类一对一

时间:2011-03-22 08:45:55

标签: nhibernate fluent-nhibernate automapping one-to-one

假设我有课程:

class Person
{
 String FirstName;
 String LastName;
 Pet Pet;
}

class Pet
{
 String Name;
 Person Owner;
}

class Cat : Pet
{
 Int32 MousesCaught;
}

class Dog : Pet
{
 Int32 CatsCaught;
}

和映射:

public class PetMap : IAutoMappingOverride<Pet>
{
    public void Override(AutoMapping<Pet> mapping)
    {
        mapping.HasOne(x => x.Owner).PropertyRef(x => x.Pet).Constrained().Cascade.All();
        mapping.JoinedSubClass<Cat>("PetId");
        mapping.JoinedSubClass<Dog>("PetId");
    }
}

问题是:如果DB中有Cat或Dog的Person,Person.Pet的类型是'Pet {PetBroxyBlaBlaBla}'。所以我不能将Person.Pet转换为类型Cat(Person.Pet为Cat == null)。

    var person = this.Session.Get<Person>(personId);
    // person.Pet as Cat == null

但是如果我在获得Person之前从DB获得此宠物,则该类型有效:

    var a = new this.Session.Get<Pet>(petId);
    var person = this.Session.Get<Person>(personId);
    // person.Pet as Cat != null

是否有办法告诉NHibernate使用有效类型初始化此属性?

1 个答案:

答案 0 :(得分:1)

尝试禁用延迟加载。它可能由this

引起