我正在尝试使用NHibernate进行数据访问,我有两个简单的实体,如下所示:

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; }
   }
}

我的问题......如果我想在其Roles集合中找到任何包含ID为1的角色的用户,我该如何构建Criteria?

有帮助吗?

解决方案

没关系,这最终是相对直接的做法:

// role was the role passed in to my Find method.
var criteria = DetachedCriteria.For(typeof (User))
            .CreateCriteria("Roles")
            .Add(Restrictions.Eq("ID", role.ID));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top