0

i'm using automapping with fluent nHibernate, very simply, like so:

Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
            .ConnectionString(c => c
                .Server("(local)\\sql2008")
                .Database("nHibernate_test")
                .TrustedConnection()))
            .Mappings(m => m.AutoMappings.Add(
                AutoMap.AssemblyOf<Domain.Airport>(cfg)
                .UseOverridesFromAssemblyOf<ReaderMappingOverride>()
                ))

my overriding classes are something like that:

public class ReaderMappingOverride : IAutoMappingOverride<Domain.Reader>
{
    public void Override(AutoMapping<Domain.Reader> mapping)
    {
        //use the reader ID as identifier of the class, instead of the ID field defined in superclass Entity
        mapping.IgnoreProperty(r => r.Id);
        mapping.Id(r => r.ReaderNumber);
    }
}

where Reader is an abstract base-class. if I use a seperate overriding classes for each sub-class it works OK. Is there any way to define the overriding for all subclasses of the abstract class?

thanks,
Jhonny

4

1 回答 1

0

好的,刚刚回答了我自己的问题——我的问题是我试图将一个从 Reader 类开始的层次结构映射到一个表中。但自动映射会自动忽略所有抽象类。我所做的只是将其添加到配置部分:

.Mappings(m => m.AutoMappings.Add(
                AutoMap.AssemblyOf<Domain.Airport>(cfg)
                .IncludeBase<Domain.Reader>()

这对我的配置类

public override bool IsDiscriminated(Type type)
    {
       //this line indicates that the readers heirarchy should be all in one table, instead of seperate tables for every type of reader
        bool ret = type.IsSubclassOf(typeof(Domain.Reader)) || type == typeof(Domain.Reader) ;
        return ret;
    }

(顺便说一句,Fluent nHibernate 网站中给出的示例使用了.net 3.5 中不存在的方法“type.In(...”...)
效果很好。
希望这会有所帮助...

于 2010-12-02T09:48:57.937 回答