注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Cisco IOS下载
 帮助

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射


2006-04-10 08:17:00
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67662
摘要:多对多的关系在日常开发中也会经常遇到,在ActiveRecord中我们用HasAndBelongsToMany特性来实现Many-Many的关联,本文将通过一个具体的实例来介绍这一用法。
 
主要内容
1.准备数据库表
2.编写实体类
3.编写测试代码
 
一.准备数据库表
接着在上篇文章中的例子,为了实现多对多的关系,我们引入Community,即每个Blog可以属于多个社区,每个社区也可以有多个Blog
CREATE TABLE Blogs (

    blog_id     
int IDENTITY(11PRIMARY KEY,

    blog_name   
varchar(50),

    blog_author 
varchar(50)

)

 

CREATE TABLE Blog_Community (

     blog_Id 
int NOT NULL ,

     community_Id 
int NOT NULL 

 )

 

 
CREATE TABLE Communities (

     community_Id 
int IDENTITY (11PRIMARY KEY,

     community_Name 
varchar (50) ,

     community_Intro 
varchar (500

 )
 
二.编写实体类代码
为了实现多对多的关系,我们要在BlogCommunity类中分别使用HasAndBelongsToMany特性,不需要编写Blog_Community类。示例代码:
// Blog

[HasAndBelongsToMany( 
typeof(Community), 

        Table
="Blog_Community"

        ColumnRef
=" community_id "

        ColumnKey
=" blog_id " )]

public IList Communitys

{

    
get return _community; }

    
set { _ community = value; }

}


 

// Community

[HasAndBelongsToMany( 
typeof(Blog), 

        Table
="Blog_Community"

        ColumnRef
="blog_id"

        ColumnKey
="community_id" )]

public IList Blogs

{

    
get return _blog; }

    
set { _ blog = value; }

}

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
指定一个附加SQLWhere子句
Where="IsPost = 0"
Lazy
指定是否延迟加载关联对象
Lazy=true|false
 
Cascade的类型值有如下几种
类型
说明
None
不进行级联操作
SaveUpdate
进行级联Save/Update操作
Delete
进行级联Delete操作
All
进行级联Save/Update/Delete操作
AllDeleteOrphan
进行级联Save/Update/Delete操作,并删除无相关父对象的子对象
 
最后完整的实体类如下:
/// <summary>

/// Blog 的摘要说明。

/// </summary>


[ActiveRecord(
"Blogs")]

public class Blog : ActiveRecordBase

{

    
private int _id;

 

    
private String _name;

 

    
private String _author;

 

    
private IList _community;

 

    [PrimaryKey(PrimaryKeyType.Identity, 
"blog_id")]

    
public int Id

    
{

        
get return _id; }

        
set { _id = value; }

    }


 

    [Property(
"blog_name")]

    
public String Name

    
{

        
get return _name; }

        
set { _name = value; }

    }


 

    [Property(
"blog_author")]

    
public String Author

    
{

        
get return _author; }

        
set { _author = value; }

    }


    

    [HasAndBelongsToMany(
typeof(Community),

            Table
="Blog_Community",

            ColumnRef
=" community_id ",

            ColumnKey
=" blog_id " )]

    
public IList Communities

    
{

        
get return _community; }

        
set { _community = value; }

    }


 

 

    
public static void DeleteAll()

    
{

        DeleteAll( 
typeof(Blog) );

    }


 

    
public static Blog[] FindAll()

    
{

        
return (Blog[]) FindAll( typeof(Blog) );

    }


 

    
public static Blog Find(int id)

    
{

        
return (Blog) FindByPrimaryKey( typeof(Blog), id );

    }


}

/// <summary>

/// Community 的摘要说明。

/// </summary>


[ActiveRecord(
"Communities")]

public class Community : ActiveRecordBase