如果有表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中。

相关文章:

  • 2021-11-24
  • 2021-06-29
  • 2022-12-23
  • 2022-01-04
  • 2021-07-21
  • 2022-12-23
  • 2021-06-29
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2022-02-14
  • 2021-06-15
  • 2022-01-15
  • 2022-01-04
  • 2021-09-29
相关资源
相似解决方案