如何映射没有表的父类的子类

时间:2013-07-13 17:16:51

标签: .net nhibernate nhibernate-mapping

我的数据库中有两个表,用于超类和子类的主数据。但是,在我的应用程序中,我在两者之间有一个中间类,它从底部子类加载数据......

abstract public class TierSettings
{
    public Guid id { get; set; }
    public string SomeTierSetting { get; set; }
}

abstract public class PricingStrategy : TierSettings
{
    public string StrategySetting1 { get; set; }
    public string StrategySetting2 { get; set; }
}

public class FirstPricingStrategy : PricingStrategy 
{
    public PricingStrategyType Strategy { get { return PricingStrategyType.Strategy1; } }
    public Decimal Value { get; set; }
}

public class SecondPricingStrategy : PricingStrategy 
{
    public PricingStrategyType Strategy { get { return PricingStrategyType.Strategy2; } }
    public Decimal Value { get; set; }
}

在我的数据库中,我只有2个表:

TierSettings(
    Id Guid Primary Key,
    SomeTierSetting NVarChar(100)
)

PricingStrategies(
    Id Guid Primary Key, Foreign Key References Parent(Id),
    StrategySetting1 NVarChar(100),
    StrategySetting2 NVarChar(100),
    StrategyType Int,
    Value Float
)

我在hbm.xml文件中映射关系时遇到问题。如果我将各个PricingStrategy类映射为扩展层,我无法访问StrategySetting属性...但我的PricingStrategy类不会直接映射到数据库。

我需要能够查询:

var items = from items in session.Query<TierSettings>() select item;

foreach(var item in items)
{
    Console.WriteLine(item.Value);
}

如何相应地构建我的映射文件?

1 个答案:

答案 0 :(得分:0)

查看'每个具体子类的表'模式

Table per concrete subclass pattern