【发布时间】:2011-05-13 11:51:39
【问题描述】:
我正在使用 Castle ActiveRecord 重新组织我们的数据库访问。我已经通过博客/帖子示例来了解事情是如何完成的,但我有一个关于 HasMany 属性的基本问题。请参阅以下示例:
课堂博客:
[ActiveRecord]
public class Blog : ActiveRecordBase
{
private int id;
private IList posts = new ArrayList();
public Blog()
{
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[HasMany(typeof(Post), Table="Posts", ColumnKey="blogid",
Inverse=true, Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList Posts
{
get { return posts; }
set { posts = value; }
}
}
班级帖子:
[ActiveRecord]
public class Post : ActiveRecordBase
{
private int id;
private String contents;
private Blog blog;
public Post()
{
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[Property(ColumnType="StringClob")]
public String Contents
{
get { return contents; }
set { contents = value; }
}
[BelongsTo("blogid")]
public Blog Blog
{
get { return blog; }
set { blog = value; }
}
}
当我现在创建博客并向该博客添加一些帖子时,为什么 Blog.Posts 集合没有自动更新?这是代码:
using (new SessionScope())
{
Blog b = new Blog();
b.Save();
Post p = new Post();
p.Blog = b;
p.Save();
//Here, b.Posts is empty, but shouldn't it contain a reference to p?
}
可以做些什么来防止这种行为?我必须手动将帖子添加到集合中吗?这里有哪些最佳做法?
TIA
【问题讨论】:
标签: nhibernate castle-activerecord