1

我感觉很笨。

public class Uber
{
   public Foo Foo { get; set; }
   public Bar Bar { get; set; }
}

public class Foo
{
   public string Name { get; set; }
}

...

var ubercharged = session.CreateCriteria(typeof(Uber))
.Add(Expression.Eq("Foo.Name", "somename"))
.UniqueResult<Uber>();
return ubercharged;

这会引发“无法解析属性”错误。

我究竟做错了什么?我想查询一个 Uber 对象,该对象的属性 Foo 的名称为“somename”。

使用流利的 nhibernate 更新了现实生活中的示例,存储库调用:

    public UserPersonalization GetUserPersonalization(string username)
    {
        ISession session = _sessionSource.GetSession();

        var personuser = session.CreateCriteria(typeof(UserPersonalization))
            .Add(Expression.Eq("User.Username", username))
            .UniqueResult<UserPersonalization>();
        return personuser;

    }

类/映射:

public class User
{
    public virtual Guid UserId { get; set; }
    public virtual string Username { get; set; }
    public virtual string Email { get; set; }
    public virtual string PasswordHash { get; set; }
    public virtual string PasswordSalt { get; set; }
    public virtual bool IsLockedOut { get; set; }
    public virtual bool IsApproved { get; set; }

}

public class Person
{
    public virtual int PersonId { get; set; }
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }

}

public class UserPersonalization
{
    public virtual int UserPersonalizationId { get; set; }
    public virtual Person Person { get; set; }
    public virtual User User { get; set; }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserId).GeneratedBy.Guid().ColumnName("UserId");
        Map(x => x.Username);
        Map(x => x.PasswordHash);
        Map(x => x.PasswordSalt);
        Map(x => x.Email);
        Map(x => x.IsApproved);
        Map(x => x.IsLockedOut);
    }
}

public class UserPersonalizationMap : ClassMap<UserPersonalization>
{
    public UserPersonalizationMap()
    {
        WithTable("UserPersonalization");
        Id(x => x.UserPersonalizationId).ColumnName("UserPersonalizationId");
        References(x => x.Person).ColumnName("PersonId");
        References(x => x.User).ColumnName("UserId");
    }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.PersonId).ColumnName("PersonId");
        Map(x => x.Name);
        References(x => x.Company).ColumnName("CompanyId");
    }
}
4

2 回答 2

5

尝试这个:

var ubercharged = session.CreateCriteria(typeof(Uber))
        .CreateCriteria("Foo")
            .Add(Restrictions.Eq("Name", "somename"))
            .UniqueResult<Uber>();
于 2009-05-07T19:30:35.897 回答
1

Can you sort using "ubercharged.AddOrder(Order.asc("Foo.Name")) syntax? This syntax should work in NHib 2.01. If not, your maps are not working correctly.

Stuart's answer should work fine for you though.

于 2009-05-07T19:35:46.273 回答