Castle ActiveRecord的一对多问题

如果有表Blog和Posts之间有一对多关系,那么如果二者关系对应外键属性代码如下所示:

        //一对多的一方添加以下字段和属性
        private IList<Post> posts=new List<Post>();
        [HasMany(Table="Posts", ColumnKey="blogid", Inverse=true, Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
        public IList<Post> Posts
        {
            get { return posts=new List<Post>(); }
            set { posts=value; }
        }
则在使用以下代码时
            Blog blog = Blog.Find(1);//Blog表中有Id值为1的记录

会引发未处理的异常:

 HibernateException

详细信息为:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: ActiveRecordDemo.Blog.Posts
 

如果把Blog中的外键关系属性代码改为:

        //一对多的一方添加以下字段和属性
        private IList<Post> posts=new List<Post>();
        [HasMany(Table="Posts", ColumnKey="blogid", Inverse=true, Cascade=ManyRelationCascadeEnum.All)]
        public IList<Post> Posts
        {
            get { return posts=new List<Post>(); }
            set { posts=value; }
        }
 则
Blog blog = Blog.Find(1);

并不会引发异常。

 

此外,Blog实例中有属性:posts,此属性应对应Blog实例的子表中记录实体,但实际却为空(此时子表posts中有对应的子记录) ,不知如何得到子记录并存放在属性posts中。

转载于:https://www.cnblogs.com/Rising/archive/2012/02/27/2370557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值