0

我一直在尝试使用 NHinernate 从我的数据库中检索特定字段。问题是我的用户列表一直返回 null。

我该怎么办?这是我的代码:

public IList<Users> GetHospitalStaff(string name) {
      ISession session = NHibernateHelper.OpenSession();
      ICriteria crt = session.CreateCriteria<Users>();
      crt.Add(Restrictions.Eq("HospitalName", name));
      crt.Add(!Restrictions.Eq("Role", "admin"));
      IList<Users> users = crt.SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"))
        .Add(Projections.Property("Email"))
        .Add(Projections.Property("Telephone"))
        .Add(Projections.Property("DateOfBirth"))
        .Add(Projections.Property("HospitalName"))
        .Add(Projections.Property("Role")))
        .SetResultTransformer(new NHibernate.Transform.AliasToBeanResultTransformer(typeof(Users)))
        .List<Users>();

      session.Close();
      return users;
}
4

1 回答 1

0

可能,您必须使用Projection.Alias()

Projections.ProjectionList()
    .Add(Projections.Alias(Projections.Property("Name"), "Name"))
    .Add(Projections.Alias(Projections.Property("Email"), "Email"))
    .Add(Projections.Alias(Projections.Property("Telephone"), "Telephone"))
    .Add(Projections.Alias(Projections.Property("DateOfBirth"), "DateOfBirth"))
    .Add(Projections.Alias(Projections.Property("HospitalName"), "HospitalName"))
    .Add(Projections.Alias(Projections.Property("Role"), "Role"))

将您的字段映射回来。

或者只是指定.Add()方法的第二个参数:

Projections.ProjectionList()
    .Add(Projections.Property("Name"), "Name")
    .Add(Projections.Property("Email"), "Email")
    .Add(Projections.Property("Telephone"), "Telephone")
    .Add(Projections.Property("DateOfBirth"), "DateOfBirth")
    .Add(Projections.Property("HospitalName"), "HospitalName")
    .Add(Projections.Property("Role"), "Role")
于 2012-04-25T12:29:35.807 回答