ActiveRecord学习笔记(四):处理Many-To-Many映射

  本文主要描述了如何使用Castle.ActiveRecord处理Many-To-Many映射。本文主要涉及了两个类:Student(学生)、Subject(学科),这两个类的关系是多对多的,因为一个学生学习多个学科,一个学科可以被多个学生学,下面是类图:


主要内容:
1.编写数据库脚本
2.HasAndBelongsToMany属性说明
3.编写实体类
4.编写调用代码

一、编写数据库脚本
  由于Student与Subject是多对多关系,这里加入一个关联表Student_Subject来保存这些关系
None.gif Create   Table   [ Student ]
None.gif(
None.gif  ID 
int   identity ( 1 , 1 primary   key ,
None.gif  StudentName 
Varchar ( 50 not   null
None.gif)
None.gif
None.gif
Create   Table   [ Subject ]
None.gif(
None.gif  ID 
int   identity ( 1 , 1 primary   key ,
None.gif  SubjectName 
Varchar ( 50 not   null
None.gif)
None.gif
None.gif
Create   Table   [ Student_Subject ]
None.gif(
None.gif  StudentID 
int   not   null ,
None.gif  SubjectID 
int   not   null ,
None.gif)

二、HasAndBelongsToMany属性说明
  在Castle.ActiveRecord中用HasAndBelongsToMany属性处理多对多关系,该属性必须包含以下三个子属性:
  1.Table:指出关联表表名(本文为Student_Subject);
  2.ColumnKey:指出关联表中指向本实体类数的列名;
  3.ColumnRef:指出关联表中指向另一个实体类的列名。
None.gif // Subject.cs
None.gif
// Table指出关联表:Student_Subject
None.gif
// ColumnKey:指出关联表(Student_Subject)中指向本实体类(Subject)的列名(SubjectID)
None.gif
// ColumnRef:指出关联表(Student_Subject)中指向另一实体类(Student)的列名(StudentID)
None.gif
[HasAndBelongsToMany( typeof (Student), Table  =   " Student_Subject " , ColumnRef  =   " StudentID " , ColumnKey  =   " SubjectID " )]
None.gif
public  IList StudentList
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
get
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return ilStudent;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
set
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ilStudent 
= value;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  除了以上三个必须子属性外,HasAndBelongsToMany属性还有以下几个常用子属性:
  1.Cascade:指明哪些操作会从父对象级联到关联的对象。该属性值应为CascadeEnum枚举值之一:
    a) None(默认值):不进行级联操作;
    b) All:表示父对象的任何操作都会关联到级联对象;
    c) Delete:表示只有对父对象进行删除操作时才会关联到级联对象;
    d) SaveUpdate:表示只有对父对象进行保存、更新操作时才会关联到级联对象。
  2.Inverse:指定是否进行级联操作;
  3.Schema:指定Schema名;
  4.Where:指定一个附加SQL的Where子句,这里应该写HQL语句;
  5.Lazy:指定是否延迟加载级联对象。

三、编写实体类
  Student.cs:
ExpandedBlockStart.gif ContractedBlock.gif /**/ ///jailusd@hotmail.com
ExpandedBlockEnd.gif
///2006-09-24

None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
using  Castle.ActiveRecord;
None.gif
None.gif
namespace  ManyToMany
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord]
InBlock.gif    
public class Student : ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intID;
InBlock.gif        
private string strName;
InBlock.gif        
private IList ilSubject;
InBlock.gif
InBlock.gif        
public Student()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            intID 
= 0;
InBlock.gif            strName 
= string.Empty;
InBlock.gif            ilSubject 
= new ArrayList();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [PrimaryKey(PrimaryKeyType.Identity,
"ID")]
InBlock.gif        
public int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return intID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intID 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Property(
"StudentName")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//Table指向关联表:Student_Subject
InBlock.gif        
//ColumnKey:指出关联表(Student_Subject)中指向本实体类(Student)的列名(StudentID)
InBlock.gif        
//ColumnRef:指出关联表(Student_Subject)中指向另一实体类(Subject)的列名(SubjectID)
InBlock.gif
        [HasAndBelongsToMany(typeof(Subject), Table = "Student_Subject", ColumnRef = "SubjectID", ColumnKey = "StudentID")]
InBlock.gif        
public IList SubjectList
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ilSubject;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ilSubject 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  Subject.cs:
ExpandedBlockStart.gif ContractedBlock.gif /**/ ///jailusd@hotmail.com
ExpandedBlockEnd.gif
///2006-09-24
None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
using  Castle.ActiveRecord;
None.gif
None.gif
namespace  ManyToMany
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord]
InBlock.gif    
public class Subject : ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intID;
InBlock.gif        
private string strName;
InBlock.gif        
private IList ilStudent;
InBlock.gif
InBlock.gif        
public Subject()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            intID 
= 0;
InBlock.gif            strName 
= string.Empty;
InBlock.gif            ilStudent 
= new ArrayList();
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        [PrimaryKey(PrimaryKeyType.Identity,
"ID")]
InBlock.gif        
public int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return intID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intID 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 学科名
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Property("SubjectName")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//Table指向关联表:Student_Subject
InBlock.gif        
//ColumnKey:指出关联表(Student_Subject)中指向本实体类(Subject)的列名(SubjectID)
InBlock.gif        
//ColumnRef:指出关联表(Student_Subject)中指向另一实体类(Student)的列名(StudentID)
InBlock.gif
        [HasAndBelongsToMany(typeof(Student), Table = "Student_Subject", ColumnRef = "StudentID", ColumnKey = "SubjectID")]
InBlock.gif        
public IList StudentList
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ilStudent;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ilStudent 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

四、编写调用代码(只列举添加Student对象的程序)
None.gif private   void  button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IConfigurationSource source 
= System.Configuration.ConfigurationManager.GetSection("activerecord"as IConfigurationSource;
InBlock.gif    
InBlock.gif    ActiveRecordStarter.Initialize(source, 
typeof(ManyToMany.Student), typeof(ManyToMany.Subject));
InBlock.gif
InBlock.gif    
//使用事务处理
InBlock.gif
    using (TransactionScope tran = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ManyToMany.Student objStudent 
= new ManyToMany.Student();
InBlock.gif        objStudent.Name 
= "jailu";
InBlock.gif
InBlock.gif        
for (int i = 0; i < 5; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ManyToMany.Subject objSubject 
= new ManyToMany.Subject();
InBlock.gif            objSubject.Name 
= "Subject " + i.ToString();
InBlock.gif            objSubject.Create();    
//这句千万不能少,若则会出错
InBlock.gif

InBlock.gif            objStudent.SubjectList.Add(objSubject);
InBlock.gif            objSubject 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        objStudent.Save();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

   在保存Student对象objStudent时必须保证objStudent.SubjectList中的Subject对象已存在数据库中,否则是无法保存Student对象的!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值