Domanda sui criteri con NHibernate
-
05-07-2019 - |
Domanda
Sto cercando di utilizzare NHibernate per l'accesso ai dati e ho 2 entità semplici che assomigliano a questo:
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; }
}
}
La mia domanda ...... come faccio a costruire un criterio se voglio trovare un utente che contenga un ruolo con un ID 1 nella sua raccolta ruoli?
Soluzione
Non importa, questo ha finito per essere relativamente semplice da fare:
// role was the role passed in to my Find method.
var criteria = DetachedCriteria.For(typeof (User))
.CreateCriteria("Roles")
.Add(Restrictions.Eq("ID", role.ID));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow