Castle ActiveRecord学习实践(7)级联

本章来看看Castle ActiveRecord中的级联(cascade)操作

还是以post和comment为例

post.cs 修改为

   1:  [ActiveRecord("Posts")]
   2:  public class Post : ActiveRecordBase<Post>
   3:  {
   4:      [PrimaryKey("PostId")]
   5:      public int Id { get; set; }
   6:   
   7:      [Property]
   8:      public string Subject { get; set; }
   9:   
  10:      [Property]
  11:      public string Text { get; set; }
  12:   
  13:      [Property]
  14:      public DateTime DateAdded { get; set; }
  15:   
  16:      [BelongsTo("CategoryId")]
  17:      public Category Category { get; set; }
  18:   
  19:      [HasMany(Lazy=true,Cascade=ManyRelationCascadeEnum.AllDeleteOrphan,Inverse = true)]
  20:      public IList<Comment> Comments { get; set; }
  21:   
  22:      [HasAndBelongsToMany(typeof(Tag), Table = "TagPost", ColumnKey = "PostId", ColumnRef = "TagId")]
  23:      public IList<Tag> Tag { get; set; }
  24:   
  25:  }

HasMany特性添加Cascade

枚举属性

枚举  
ManyRelationCascadeEnum None 不做级联操作
  All 增删改都做级联操作
  AllDeleteOrphan 都做级联操作,并且删除孤儿数据。即删除没有对应Post的Comment对象
  SaveUpdate 在增加和更新的时候做级联操作
  Delete 在删除的时候做级联操作

1、级联添加

创建一个post ,并添加多个comment

   1:  //创建post
   2:  Post post = new Post();
   3:  post.DateAdded = DateTime.Now;
   4:  post.Subject = "castle active record 7";
   5:  post.Text = "content";
   6:  List<int> list = new List<int>();
   7:  list.Add(1);
   8:  list.Add(2);
   9:  post.Category = Category.Find(5);
  10:  post.Tag = Tag.FindAll();
  11:  using (TransactionScope tran=new TransactionScope ())
  12:  {
  13:      try
  14:      {
  15:          post.Create();
  16:          for (int i = 0; i < 10; i++)
  17:          {
  18:              //创建comment
  19:              Comment comment = new Comment();
  20:              comment.Author = "hzd" + i;
  21:              comment.DateAdded = DateTime.Now;
  22:              comment.Text = "comment" + i + " content!";
  23:              comment.Post = post;
  24:              comment.Save();
  25:          }               
  26:          tran.VoteCommit();
  27:         
  28:      }
  29:      catch 
  30:      {
  31:          tran.VoteRollBack();
  32:      } 
  33:  }

2、级联更新

为已经存在的post 添加 comment

   1:  Post post = new Post();
   2:  post = Post.Find(8);
   3:  using (TransactionScope tran=new TransactionScope ())
   4:  {
   5:      try
   6:      {
   7:          for (int i = 0; i < 5; i++)
   8:          {
   9:              //创建comment
  10:              Comment comment = new Comment();
  11:              comment.Author = "hzd-" + i;
  12:              comment.DateAdded = DateTime.Now;
  13:              comment.Text = "comment" + i + " content!";
  14:              comment.Post = post;
  15:              comment.Save();
  16:          }
  17:          post.Text = "castle active record  Cascade";
  18:          post.Update();
  19:          tran.VoteCommit();
  20:         
  21:      }
  22:      catch
  23:      {
  24:          tran.VoteRollBack();
  25:      } 
  26:  }
  27:   

3、级联删除

删除一个post

   1:  Post post = new Post();
   2:  post = Post.Find(1);
   3:  post.Delete();

删除一个post的一个comment

   1:  using (new SessionScope())
   2:  {
   3:      Post post = new Post();
   4:      post = Post.Find(8);
   5:      int count = post.Comments.Count;
   6:      post.Comments.RemoveAt(0);
   7:      post.Update();
   8:  }

特别注意

   1:  [HasMany(Lazy = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true)]
   2:  public IList<Comment> Comments { get; set; }

设置One-To-Many 中 One方(本文中的post) HasManyAttribute的Inverse属性为true

Inverse属性指定了双向关联中的所有者,它的默认值为false。

Inverse 为true是,关联关系的维护者为 主表(即posts表)。

为false时,关联关系的维护者为 子表(即comments表),如果子表设置了外键不为null,会出现

不能将值 NULL 插入列 '外键列'

的错误。

原文地址:https://www.cnblogs.com/whx1973/p/2745668.html