本文介绍了流利的NHibernate继承映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Fluent NHibernate和每个子类的继承映射。
我想引用一个特定的对象列表,但我不明白,如何将结果复制到一个特定的类的对象。

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

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

//这工作正常
HasMany(c => c.Pets);

//这是不可行的,因为结果中包含了狗和猫
//我如何告诉NHibernate只能取狗或猫?
HasMany< Pet>(c => c.Cats);
HasMany< Pet>(c => c.Dogs);
}
}

class Pet
{
int ID;
}
class Dog:Pet
{
对象DogSpecificProperty;
}
class Cat:Pet
{
object CatSpecificProperty;
}
class Person
{
int ID;
IList< Pet>宠物;
IList< Dog>小狗;
IList< Cat>猫;

code


$ b $任何人都可以帮我吗?请原谅我的英文不好。

应该看起来像这样:

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

//这工作正常
HasMany(c => c.Pets);

//这是不可行的,因为结果中包含了狗和猫
//我如何告诉NHibernate只能取狗或猫?
HasMany< Cat>(c => c.Cats);
HasMany< Dog>(c => c.Dogs);




$ b $因为你是Person有一个Dogs属性,一个IList不是IList,反过来也是一样的猫。


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

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.

这篇关于流利的NHibernate继承映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:53