程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Active Record學習筆記(四):處理Many-To-Many映射

Active Record學習筆記(四):處理Many-To-Many映射

編輯:關於.NET

本文主要描述了如何使用Castle.ActiveRecord處理Many-To-Many映射。本文主要涉及了兩個類:Student(學生)、Subject(學科),這兩個類的關系是多對多的,因為一個學生學習多個學科,一個學科可以被多個學生學,下面是類圖:

主要內容:

1.編寫數據庫腳本

2.HasAndBelongsToMany屬性說明

3.編寫實體類

4.編寫調用代碼

一、編寫數據庫腳本

由於Student與Subject是多對多關系,這裡加入一個關聯表Student_Subject來保存這些關系

Create Table [Student]
(
 ID int identity(1,1) primary key,
 StudentName Varchar(50) not null
)
  
Create Table [Subject]
(
 ID int identity(1,1) primary key,
 SubjectName Varchar(50) not null
)
  
Create Table [Student_Subject]
(
 StudentID int not null,
 SubjectID int not null,
)

二、HasAndBelongsToMany屬性說明

在Castle.ActiveRecord中用HasAndBelongsToMany屬性處理多對多關系,該屬性必須包含以下三個子屬性:

1.Table:指出關聯表表名(本文為Student_Subject);

2.ColumnKey:指出關聯表中指向本實體類數的列名;

3.ColumnRef:指出關聯表中指向另一個實體類的列名.

//Subject.cs
//Table指出關聯表:Student_Subject
//ColumnKey:指出關聯表(Student_Subject)中指向本實體類(Subject)的列名(SubjectID)
//ColumnRef:指出關聯表(Student_Subject)中指向另一實體類(Student)的列名(StudentID)
[HasAndBelongsToMany(typeof(Student), Table = "Student_Subject", ColumnRef = "StudentID", ColumnKey = "SubjectID")]
public IList StudentList
{
  get
  {
    return ilStudent;
  }
  set
  {
    ilStudent = value;
  }
}

除了以上三個必須子屬性外,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: /**////[email protected]

///2006-09-24

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
  
using Castle.ActiveRecord;
  
namespace ManyToMany
{
  [ActiveRecord]
  public class Student : ActiveRecordBase
  {
    private int intID;
    private string strName;
    private IList ilSubject;
  
    public Student()
    {
      intID = 0;
      strName = string.Empty;
      ilSubject = new ArrayList();
    }
  
    [PrimaryKey(PrimaryKeyType.Identity,"ID")]
    public int ID
    {
      get
      {
        return intID;
      }
      set
      {
        intID = value;
      }
    }
  
    [Property("StudentName")]
    public string Name
    {
      get
      {
        return strName;
      }
      set
      {
        strName = value;
      }
    }
  
    //Table指向關聯表:Student_Subject
    //ColumnKey:指出關聯表(Student_Subject)中指向本實體類(Student)的列名(StudentID)
    //ColumnRef:指出關聯表(Student_Subject)中指向另一實體類(Subject)的列名(SubjectID)
    [HasAndBelongsToMany(typeof(Subject), Table = "Student_Subject", ColumnRef = "SubjectID", ColumnKey = "StudentID")]
    public IList SubjectList
    {
      get
      {
        return ilSubject;
      }
      set
      {
        ilSubject = value;
      }
    }
  }
}

Subject.cs: /**////[email protected]

///2006-09-24

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
  
using Castle.ActiveRecord;
  
namespace ManyToMany
{
  [ActiveRecord]
  public class Subject : ActiveRecordBase
  {
    private int intID;
    private string strName;
    private IList ilStudent;
  
    public Subject()
    {
      intID = 0;
      strName = string.Empty;
      ilStudent = new ArrayList();
    }
  
    [PrimaryKey(PrimaryKeyType.Identity,"ID")]
    public int ID
    {
      get
      {
        return intID;
      }
      set
      {
        intID = value;
      }
    }
  
    /**//// <summary>
    /// 學科名
    /// </summary>
    [Property("SubjectName")]
    public string Name
    {
      get
      {
        return strName;
      }
      set
      {
        strName = value;
      }
    }
  
    //Table指向關聯表:Student_Subject
    //ColumnKey:指出關聯表(Student_Subject)中指向本實體類(Subject)的列名(SubjectID)
    //ColumnRef:指出關聯表(Student_Subject)中指向另一實體類(Student)的列名(StudentID)
    [HasAndBelongsToMany(typeof(Student), Table = "Student_Subject", ColumnRef = "StudentID", ColumnKey = "SubjectID")]
    public IList StudentList
    {
      get
      {
        return ilStudent;
      }
      set
      {
        ilStudent = value;
      }
    }
  }
}

四、編寫調用代碼(只列舉添加Student對象的程序)

private void button1_Click(object sender, EventArgs e)
{
  IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
  
  ActiveRecordStarter.Initialize(source, typeof(ManyToMany.Student), typeof(ManyToMany.Subject));
  
  //使用事務處理
  using (TransactionScope tran = new TransactionScope())
  {
    ManyToMany.Student objStudent = new ManyToMany.Student();
    objStudent.Name = "jailu";
  
    for (int i = 0; i < 5; i++)
    {
      ManyToMany.Subject objSubject = new ManyToMany.Subject();
      objSubject.Name = "Subject " + i.ToString();
      objSubject.Create();  //這句千萬不能少,若則會出錯
  
      objStudent.SubjectList.Add(objSubject);
      objSubject = null;
    }
  
    objStudent.Save();
  }
}

在保存Student對象objStudent時必須保證objStudent.SubjectList中的Subject對象已存在數據庫中,否則是無法保存Student對象的!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved