While adding post to the post repository an exception occurs with the following message: not null property references a null or transient value Category.
[Test]
public void PostInsertion()
{
var category = new Category
{
Title = "Programming",
Description = "Programming"
};
var post = new Post
{
AuthorUrl = "some url",
Category = category,
Content = "some content",
Feedbacks = new HashedSet<Feedback>(),
Timestamp = DateTime.Now,
Title = "some title"
};
var postRepository = new Repository<Post>(this.sessionFactory);
postRepository.Add(post);
}
What does it mean?
EDIT: Post entity definition
[Serializable]
public class Post : Entity<Post>
{
public Post()
{
this.Feedbacks = new HashedSet<Feedback>();
}
public virtual String Title { get; set; }
public virtual String Content { get; set; }
public virtual DateTime Timestamp { get; set; }
public virtual Byte[] Thumbnail { get; set; }
public virtual Byte[] AuthorImg { get; set; }
public virtual String AuthorUrl { get; set; }
public virtual Category Category { get; set; }
public virtual ISet<Feedback> Feedbacks { get; set; }
public virtual void AddFeedback(Feedback feedback)
{
this.Feedbacks.Add(feedback);
}
}
Thanks!
That exception was thrown because NHibernate tried to add a reference to a Category that was not persisted on the database. In you example you are creating a new Category, so you have two options:
For the second option, if you are using Xml mapping, then you’ll need something like:
If you are using Fluent NHibernate, then it’ll be: