3

我将 Nhibernate 与 SQL Server 2008 一起使用。

我正在尝试执行以下代码:

        var localization = session.QueryOver<T>()
            .Where(idFilter).AndRestrictionOn(x => x.Language.IETFTag).IsLike(tag + "%").SingleOrDefault();

但是,我在该行上遇到一个异常,说 nhibernate 无法解析属性 Language.IETFTag (或类似的东西)。

我曾尝试使用 JoinQueryOver() 但它抱怨我在 FROM 子句中有多个相关性或类似的奇怪东西。只是觉得我做错了什么。我怎样才能做我想做的事?

我有以下映射:

internal class LocalizationMapping : ClassMap<Localization>
{
    public LocalizationMapping()
    {
        UseUnionSubclassForInheritanceMapping();
        Id(x => x.Id).GeneratedBy.HiLo("HiLo", "NextHi", "1000");
        References(x => x.Language);
    }
}

internal class LanguageMapping : ClassMap<Language>
{
    public LanguageMapping()
    {
        Id(x => x.Id);
        Map(x => x.DefaultName);
        Map(x => x.IETFTag);
    }
}

internal class ArticleLocalizationMapping : SubclassMap<ArticleLocalization>
{
    public ArticleLocalizationMapping()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.Article);
    }
}
4

1 回答 1

8

您需要事先使用别名连接到“语言”表。完整的查询应该是:

Language language = null;

var localization = session.QueryOver<Localization>()
            .JoinAlias(x => x.Language, () => language)
            .Where(idFilter)
            .AndRestrictionOn(() => language.IETFTag).IsLike(tag, MatchMode.End)
            .SingleOrDefault();

更多信息请访问: http: //nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

Also, the "IsLike" method accepts a second arg with the matching type. I took the liberty to update your instruction with it.

于 2011-04-27T00:00:48.557 回答