I have been trying to retrieve particular fields from my database using NHinernate. The problem is that my users list keeps returning null.
What do I do? Here is my code:
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;
}
Probably, you have to use
Projection.Alias():to map your fields back.
Or just specify second parameter of
.Add()method: