博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Castle ActiveRecord学习实践(7)级联
阅读量:4677 次
发布时间:2019-06-09

本文共 3527 字,大约阅读时间需要 11 分钟。

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

还是以post和comment为例

post.cs 修改为

1:  [ActiveRecord("Posts")]
2:  public class Post : ActiveRecordBase
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
Comments { get; set; }
21:   
22:      [HasAndBelongsToMany(typeof(Tag), Table = "TagPost", ColumnKey = "PostId", ColumnRef = "TagId")]
23:      public IList
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
list = new List
();
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
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/archive/2012/10/30/2745668.html

你可能感兴趣的文章
Perl 算号器
查看>>
Django SimpleCMDB 使用序列化
查看>>
update()
查看>>
如何利用Github+Appveyor+Nuget打造自己的.net core开源库
查看>>
VS2012编译LibZip库
查看>>
置顶准时[置顶] 程序员也有风骚的青春
查看>>
执行测试Exchange 2010 快速清空一个邮箱中的数据内容!
查看>>
统计次数poj1218——THE DRUNK JAILER
查看>>
Linux 内核进程管理之进程ID 。图解
查看>>
GiB与GB
查看>>
mysql 中间件
查看>>
在gdb将所有线程的堆栈输出到文件中去
查看>>
b继承a
查看>>
2017年10月17日
查看>>
20145204《信息安全系统设计基础》第3周学习总结
查看>>
Longest Common Prefix
查看>>
InstallShield 2012 Spring新功能试用总结
查看>>
二进制转换,八进制,十六进制转换
查看>>
Django项目部署
查看>>
新的一年,让我们携手前行
查看>>