Can't get Fluent nHibernate to work with Subclasses

落爺英雄遲暮 提交于 2019-12-13 08:27:44

问题


So I would like to have a BaseEntity which contains common columns that appear in all tables and use inheritance to keep mapping of these fields in one place. For the sake of simplicity I will only include one field (Id). The code looks as follows:

[Serializable]
public abstract class BaseEntity
{
    public Guid Id { get; set; }
}

public class Hedge : BaseEntity
{
    public virtual DateTime HedgeDate { get; set; }
    public virtual DateTime SettleDate { get; set; }
}

public class Trade : BaseEntity
{
    public virtual DateTime TradeDate { get; set; }
    public virtual DateTime SettleDate { get; set; }
}

The Mapping looks as follows:

public class BaseEntityMap : ClassMap<BaseEntity>
{
    public BaseEntityMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();
        UseUnionSubclassForInheritanceMapping();
    }
}

public class HedgeMap : SubclassMap<Hedge>
{
    public HedgeMap()
    {
        Map(x => x.HedgeDate);
        Map(x => x.SettleDate);
    }
}

public class TradeMap: SubclassMap<Trade>
{
    public TradeMap()
    {
        Map(x => x.TradeDate);
        Map(x => x.SettleDate);
    }
}

To load the configuration I am using the following code:

Configuration cfg =
                Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                                  .ConnectionString(TestDatabaseConnection.ConnectionString))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<HedgeMap>()
                    ).BuildConfiguration();

Now when I run my unit tests, the configuration is loaded and created successfully however the configuration is empty (no class maps). As far as I can tell I am following the documentation available on the fluent-nhibernate website. Does anyone know what I am doing wrong?

来源:https://stackoverflow.com/questions/8811181/cant-get-fluent-nhibernate-to-work-with-subclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!