开发者

Fluent NHibernate inheritance mapping problem

开发者 https://www.devze.com 2022-12-11 10:40 出处:网络
I am using Fluent NHibernate with table per subclass inheritance mapping. I want to reference to a list of specific objects, but i can\'t figure out, how to restict the result to objects of one specif

I am using Fluent NHibernate with table per subclass inheritance mapping. I want to reference to a list of specific objects, but i can't figure out, how to restict the result to objects of one specific class.

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<Ca开发者_运维百科t>
{
    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;
}

Can anyone help me? Please excuse my poor english.


I'm not an expert in Fluent NH, but it seems to me that your person map should look like this:

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

Because you're Person has a Dogs property which is an IList not IList, and conversely the same for Cats.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号