使用 UseOverridesFromAssemblyOf 进行自动映射不会调用基类的重写类

发布于 2024-10-05 18:47:06 字数 1033 浏览 2 评论 0原文

我正在使用流畅的 nHibernate 自动映射,非常简单,就像这样:

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>()
                ))

我的重写类是这样的:

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

其中 Reader 是一个抽象基类。 如果我为每个子类使用单独的重写类,它就可以正常工作。 有什么方法可以定义抽象类的所有子类的重写吗?

谢谢,
强尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-10-12 18:47:06

好吧,刚刚回答了我自己的问题-
我的问题是我试图将从 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(...”)。 .)
效果很好。
希望这有帮助...

ok, just answered my own question-
my problem was that i was trying to map a heirarchy which started with the Reader class, into a single table. but auto-mapping automatically ignores all abstract classes. what i did was just add this to the configuration section:

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

and this to my configuration class

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

(BTW, the example given in Fluent nHibernate's site uses the method "type.In(..." which does not exist in .net 3.5...)
that worked fine.
hopes this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文