所以我希望有一个BaseEntity,它包含出现在所有表中的公共列,并使用继承来保持这些字段在一个地方的映射。为简单起见,我将只包含一个字段(Id)。代码如下:
[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; }
}
映射如下所示:
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);
}
}
要加载配置,我使用以下代码:
Configuration cfg =
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(TestDatabaseConnection.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<HedgeMap>()
).BuildConfiguration();
现在,当我运行单元测试时,配置已加载并成功创建,但配置为空(没有类映射)。据我所知,我正在关注fluent-nhibernate网站上提供的文档。有谁知道我做错了什么?