0

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

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 的 Role 的任何用户,我该如何构造一个 Criteria?

4

1 回答 1

1

没关系,这最终变得相对简单:

// role was the role passed in to my Find method.
var criteria = DetachedCriteria.For(typeof (User))
            .CreateCriteria("Roles")
            .Add(Restrictions.Eq("ID", role.ID));
于 2009-01-07T20:47:45.850 回答