流畅的NHibernate继承映射问题

时间:2009-11-12 17:54:30

标签: nhibernate fluent-nhibernate

我正在使用Fluent NHibernate与table per subclass继承映射。 我想引用一个特定对象列表,但我无法弄清楚,如何将结果重写为一个特定类的对象。

class PetMap : ClassMap<Pet>
{
    public PetMap()
    {
        Id(c => c.ID).GeneratedBy.Identity();            
    }
}

class DogMap : ClassMap<Dog>
{
    public DogMap()
    {
       Mac(c => c.DogSpecificProperty);                                
    }
}

class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
       Mac(c => c.CatSpecificProperty);                                    
    }
}

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {  
        Id(c => c.ID).GeneratedBy.Identity();

        //this works fine
        HasMany(c => c.Pets);

        //this dosen't work, because the result contains dogs and cats
        //how can I tell NHibernate to only fetch dogs or cats?
        HasMany<Pet>(c => c.Cats);
        HasMany<Pet>(c => c.Dogs);
    }
}

class Pet
{ 
   int ID;
}
class Dog : Pet
{
   object DogSpecificProperty;
}
class Cat : Pet
{
   object CatSpecificProperty;
}
class Person
{
   int ID;
   IList<Pet> Pets;
   IList<Dog> Dogs;
   IList<Cat> Cats;
}

class PetMap : ClassMap<Pet> { public PetMap() { Id(c => c.ID).GeneratedBy.Identity(); } } class DogMap : ClassMap<Dog> { public DogMap() { Mac(c => c.DogSpecificProperty); } } class CatMap : SubclassMap<Cat> { public CatMap() { Mac(c => c.CatSpecificProperty); } } class PersonMap : ClassMap<Person> { public PersonMap() { Id(c => c.ID).GeneratedBy.Identity(); //this works fine HasMany(c => c.Pets); //this dosen't work, because the result contains dogs and cats //how can I tell NHibernate to only fetch dogs or cats? HasMany<Pet>(c => c.Cats); HasMany<Pet>(c => c.Dogs); } } class Pet { int ID; } class Dog : Pet { object DogSpecificProperty; } class Cat : Pet { object CatSpecificProperty; } class Person { int ID; IList<Pet> Pets; IList<Dog> Dogs; IList<Cat> Cats; } 谁能帮我?请原谅我可怜的英语。

1 个答案:

答案 0 :(得分:1)

我不是Fluent NH的专家,但在我看来,你的人物地图应该是这样的:

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {  
        Id(c => c.ID).GeneratedBy.Identity();

        //this works fine
        HasMany(c => c.Pets);

        //this dosen't work, because the result contains dogs and cats
        //how can I tell NHibernate to only fetch dogs or cats?
        HasMany<Cat>(c => c.Cats);
        HasMany<Dog>(c => c.Dogs);
    }
}

因为你是Person有一个Dogs属性,它是一个IList而不是IList,而Cats则相反。

相关问题