在Join列上使用流畅的Nhibernate ApplyFilter

时间:2011-03-17 21:47:43

标签: join fluent-nhibernate mapping

我正在尝试对另一个表中的Join(和投影)列应用过滤器(ApplyFilter)。我有以下实体:

public class User
{
    public virtual int Id { get; private set; }
    public virtual string EMail { get; set; }
    ...
    public virtual bool IsActive { get; set; }
    public virtual int CompanyId { get; set; }
}

使用UserMap:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("Users");
        Id(x => x.Id, "UserId");
        Map(x => x.EMail);
        ...

        Join("CompanyUser", r =>
        {
            r.KeyColumn("UserId");
            r.Map(x => x.IsActive);
            r.Map(x => x.CompanyId);
            r.Fetch.Join();

        });

        ApplyFilter<CompanyFilter>("this_1_.CompanyId = :companyId");
    }

这实际上现在正常工作,但正如您所看到的,我必须在过滤器中为CompanyUser表包含别名“this_1_”...这听起来不正确,但如果我将过滤器保留为在FilterDefinition类中定义我得到一个带有:

的Sql
where this.CompanyId = ?p0

未映射'cos CompanyId列来自不同的投影(CompanyUser as this_1 _)

有没有办法纠正这个问题,让nhibernate找出过滤器的正确别名?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为你必须在连接上应用过滤器:

    Join("CompanyUser", r =>
    {
        r.KeyColumn("UserId");
        r.Map(x => x.IsActive);
        r.Map(x => x.CompanyId);
        r.Fetch.Join();

    }).ApplyFilter<CompanyFilter>("CompanyId = :companyId");