多对多的关系在日常开发中也会经常遇到,在ActiveRecord中我们用HasAndBelongsToMany特性来实现Many-Many的关联,本文将通过一个具体的实例来介绍这一用法。
1.准备数据库表
2.编写实体类
3.编写测试代码
一.准备数据库表
接着在上篇文章中的例子,为了实现多对多的关系,我们引入Community,即每个Blog可以属于多个社区,每个社区也可以有多个Blog。
CREATE TABLE Blogs (
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
blog_id int IDENTITY(1, 1) PRIMARY KEY,
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
blog_name varchar(50),
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
blog_author varchar(50)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE Blog_Community (
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
blog_Id int NOT NULL ,
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
community_Id int NOT NULL
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE Communities (
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
community_Id int IDENTITY (1, 1) PRIMARY KEY,
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
community_Name varchar (50) ,
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
community_Intro varchar (500)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
)
二.编写实体类代码
为了实现多对多的关系,我们要在Blog、Community类中分别使用HasAndBelongsToMany特性,不需要编写Blog_Community类。示例代码:
// ![](https://terrylee.cnblogs.com/Images/dot.gif)
Blog
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[HasAndBelongsToMany( typeof(Community),
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
Table="Blog_Community",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
ColumnRef=" community_id ",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
ColumnKey=" blog_id " )]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public IList Communitys
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _community; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _ community = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
// ![](https://terrylee.cnblogs.com/Images/dot.gif)
Community
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[HasAndBelongsToMany( typeof(Blog),
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
Table="Blog_Community",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
ColumnRef="blog_id",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
ColumnKey="community_id" )]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public IList Blogs
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _blog; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _ blog = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
HasAndBelongsToMany的参数相信大家都能够看明白,指定关联表名和关联的外键就可以了。
注意:这三个参数必须指定,不可以省略!
HasManyAttribute说明
属性
|
说明
|
示例
|
Cascade
|
指明哪些操作会从父对象级联到关联的对象,相关的操作见后面,如果不指定,则为None
|
Cascade=ManyRelationCascadeEnum.All
|
Inverse
|
指定是否级联操作
|
Inverse =true|false
|
Schema
|
指定Schema的名字
|
Schema="ARDemo"
|
Table
|
指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略
|
Table="posts"
|
ColumnKey
|
本实体类于另一个实体类关联的外键
|
ColumnKey="community_id"
|
ColumnRef
|
另一实体类的外键
|
ColumnRef="blog_id"
|
Where
|
指定一个附加SQL的Where子句
|
Where="IsPost = 0"
|
Lazy
|
指定是否延迟加载关联对象
|
Lazy=true|false
|
Cascade的类型值有如下几种
类型
|
说明
|
None
|
不进行级联操作
|
SaveUpdate
|
进行级联Save/Update操作
|
Delete
|
进行级联Delete操作
|
All
|
进行级联Save/Update/Delete操作
|
AllDeleteOrphan
|
进行级联Save/Update/Delete操作,并删除无相关父对象的子对象
|
最后完整的实体类如下:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// Blog 的摘要说明。
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[ActiveRecord("Blogs")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Blog : ActiveRecordBase
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _id;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _name;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _author;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private IList _community;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PrimaryKey(PrimaryKeyType.Identity, "blog_id")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Id
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _id; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _id = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("blog_name")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Name
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _name; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _name = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("blog_author")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Author
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _author; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _author = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HasAndBelongsToMany(typeof(Community),
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Table="Blog_Community",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ColumnRef=" community_id ",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ColumnKey=" blog_id " )]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public IList Communities
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _community; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _community = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void DeleteAll()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DeleteAll( typeof(Blog) );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Blog[] FindAll()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (Blog[]) FindAll( typeof(Blog) );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Blog Find(int id)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (Blog) FindByPrimaryKey( typeof(Blog), id );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// Community 的摘要说明。
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[ActiveRecord("Communities")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Community : ActiveRecordBase
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _id;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _name;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _intro;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private IList _blog;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PrimaryKey(PrimaryKeyType.Identity, "Community_Id")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Id
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _id; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _id = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("Community_Name")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Name
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _name; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _name = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("Community_Intro")]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Author
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _intro; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _intro = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[HasAndBelongsToMany(typeof(Blog),
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Table="Blog_Community",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ColumnRef="blog_id",
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ColumnKey="community_id" )]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public IList Blogs
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _blog; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _blog = value; }
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void DeleteAll()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DeleteAll( typeof(Community) );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Community[] FindAll()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (Community[]) FindAll( typeof(Community) );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Community Find(int id)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (Community) FindByPrimaryKey( typeof(Community), id );
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
三.编写测试代码
下面是我写的一些简单的测试代码,有兴趣的可以看一下。
1.级联增加:新增一个Blog,让它同时属于好几个社区
[Test]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public void TestCascadingSave()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//新建一个Blog
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Blog blog = new Blog();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Name = "Tech Space";
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Author = "Terrylee";
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//属于ID为1,2社区
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ArrayList list = new ArrayList();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
list.Add(Community.Find(1));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
list.Add(Community.Find(2));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Communities = list;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//保存
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Save();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
2.级联更新:对一个已经存在Blog,更改它属于更多的社区
[Test]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public void TestCascadingUpdate()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//测试1:查找一个Blog
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Blog blog = Blog.Find(10);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IList clist = blog.Communities;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clist.Add(Community.Find(4));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clist.Add(Community.Find(3));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Save();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//测试2:查找一个Community
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Community community = Community.Find(3);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IList blist = community.Blogs;
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blist.Add(Blog.Find(8));
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
community.Save();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
3.级联删除:删除一个Blog,级联表中对应的记录应该删除,但Community不能删除,因为还有别的Blog和它关联
[Test]
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public void TestCascadingDelete()
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//测试1:删除Blog
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Blog blog = Blog.Find(10);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using(TransactionScope btran = new TransactionScope())
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
blog.Delete();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btran.VoteCommit();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
catch
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btran.VoteRollBack();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//测试2:删除Community
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Community community = Community.Find(3);
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using(TransactionScope ctran = new TransactionScope())
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
community.Delete();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctran.VoteCommit();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
catch
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctran.VoteRollBack();
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
好了,关于Many-Many关联映射就写这么多了,内容比较简单。下篇文章我会介绍在ActiveRecord中实现延迟加载和使用Where子句。