我正在尝试获取 aList<User>
其中包含 a List<Article>
for each User
,但是在查询时我得到 the List<User>
and every User
(the only User
) has aList<Article>
但它不包含任何内容。
我检查了我的数据库并且密钥匹配,所以我的猜测(嗯,猜测不多)是我的映射存在一些问题。我搜索了一对多(HasMany)的示例并尝试了它们,但没有任何效果。有谁知道可能出了什么问题?
课程
public class User
{
public virtual int UserId { get; set; }
public virtual string Name { get; set; }
public virtual string Password { get; set; }
public virtual IList<Article> Articles { get; set; }
public User()
{
Articles = new List<Article>();
}
}
public class Article
{
public virtual int ArticleId { get; set; }
public virtual string Title { get; set; }
public virtual string Ingress { get; set; }
public virtual DateTime Created { get; set; }
public virtual string ArticleText { get; set; }
public virtual User Author { get; set; }
}
这些是我的映射
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId);
Map(x => x.Name);
Map(x => x.Password);
HasMany(x => x.Articles).KeyColumn("UserId").LazyLoad();
}
}
public class ArticleMap : ClassMap<Article>
{
public ArticleMap()
{
Id(x => x.ArticleId);
Map(x => x.Title);
Map(x => x.Ingress);
Map(x => x.Created);
References(x => x.Author).Column("UserId").LazyLoad();
Map(x => x.ArticleText).Length(4001);
}
}
这是我插入/选择的电话
List<User> usr = new List<User>();
using (var session = SessionFactoryHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
User u = new User
{
Name = "Mohindra",
Password = "Corn"
};
Article a = new Article
{
Title = "Doh!",
Ingress = "yada yada",
ArticleText = "why dammit! whyyy?!11!!",
Created = DateTime.Now,
Author = u
};
session.Save(u);
session.Save(a);
usr = session.Query<User>().ToList();
transaction.Begin();
transaction.Commit();
}
}
这可能是最简单的新手错误,但我对使用 O/RM 非常陌生,所以我不知道这里可能出现什么问题。