I am trying to get a List<User> which in turn contains a List<Article> for every User, however when querying i get the List<User> and every User (the only User) has a List<Article> but it doesn’t contain anything.
I’ve checked my DB and the keys match, so my guess (well ,not much of a guess) is that there is some issue in my mapping. I’ve searched for examples for one-to-many (HasMany) and tried them but nothing’s working. Anyone who have any idea what might be wrong?
Classes
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; }
}
These are my mappings
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);
}
}
And here is my call to insert / select
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();
}
}
This might be the simplest of rookie mistakes but I am very new to using O/RM’s so i don’t know what might be the issue here.

Used,
HasMany(x => x.Articles).Table("Article").KeyColumn("UserId").LazyLoad();And then I saved my objects independently.
When I should have used,
HasMany(x => x.Articles).Table("Article").KeyColumn("UserId").Cascade.All();^ In this case the
Articleinherited myUserand I could then save them with the relation.