NHibernate一对多关系属性

时间:2018-06-26 18:19:05

标签: c# nhibernate fluent-nhibernate

我在对某个类的一对多属性进行分页时遇到问题。这些是我尝试从数据库中提取的实体及其映射:

class Author
{
     public virtual int id { get; set; }
     public virtual string name { get; set; } 
     public virtual IList<Book> books { get; set; }
}

class Book 
{
     public virtual int id { get; set; }
     public virtual string name { get; set; }
}   

class AuthorMap : ClassMap<Author>
{
     Table('Authors')
     Id(x => x.id)
     Map(x => x.name)
     HasMany(x => x.books)
}

class BookMap : ClassMap<Book>
{
     Table('Books')
     Id(x => x.id)
     Map(x => x.name)
}

如何从数据库中拉取特定作者并查询与该作者相关的书籍?

1 个答案:

答案 0 :(得分:0)

尝试一下:

var authorWithBooks = session.Query<Author>().Where(x => x.Name == "...").Fetch(x => x.Books).Single();