Pregunta de criterios con NHibernate
-
05-07-2019 - |
Pregunta
Estoy tratando de usar NHibernate para el acceso a datos, y tengo 2 entidades simples que se parecen a esto:
public class User : IEntity
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Logon { get; set; }
public string Password { get; set; }
public ICollection<Role> Roles { get; set; }
public bool IsNew
{
get { return (ID == 0) ? true : false; }
}
public User()
{
Roles = new List<Role>();
}
}
public class Role : IEntity
{
public int ID { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public bool IsNew
{
get { return (ID == 0) ? true : false; }
}
}
Mi pregunta ...... ¿cómo construyo un Criterio si quiero encontrar algún usuario que contenga un Rol con un ID de 1 en su colección de Roles?
Solución
No importa, esto terminó siendo relativamente sencillo de hacer:
// role was the role passed in to my Find method.
var criteria = DetachedCriteria.For(typeof (User))
.CreateCriteria("Roles")
.Add(Restrictions.Eq("ID", role.ID));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow