ActiveRecord学习笔记(五):处理继承关系

  本文主要介绍了如何使用Castle.ActiveRecord来处理继承关系。
  本文涉及两个实体类:基类(User)、子类(Employee)。以下是类图:


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

一、编写数据库脚本
  其实本文涉及的数据表在前面的笔记中都出现过!
None.gif Create   Table  Users
None.gif(
None.gif  ID 
int   identity ( 1 , 1 primary   key ,
None.gif  LoginName 
Varchar ( 50 not   null ,
None.gif  Password 
Varchar ( 20 not   null
None.gif)
None.gif
None.gif
Create   Table  Employees
None.gif(
None.gif  ID 
int   primary   key ,
None.gif  Salary 
money
None.gif)

二、JoinedBase和JoinedKey属性说明
  JoinedBase属性:该属性是ActiveRecord属性子属性,用于基类中;
  JoinedKey性性:该属性用在子类中,用于代替PrimaryKey的位置;

三、编写实体类
  基类:User.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
None.gif
using  Castle.ActiveRecord;
None.gif
None.gif
namespace  Inherit
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord(
"Users"),JoinedBase]  //JoinedBase
InBlock.gif
    public class User : ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intID;
InBlock.gif        
private string strUserName;
InBlock.gif        
private string strUserPsd;
InBlock.gif
InBlock.gif        
public User()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            intID 
= 0;
InBlock.gif            strUserName 
= string.Empty;
InBlock.gif            strUserPsd 
= string.Empty;
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("LoginName")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return strUserName;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用户密码
ExpandedSubBlockEnd.gif        
/// </summary>

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

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  子类:Employee.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
None.gif
using  Castle.ActiveRecord;
None.gif
None.gif
namespace  Inherit
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ActiveRecord(
"Employees")]
InBlock.gif    
public class Employee : User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int intEID;
InBlock.gif        
private decimal decimalSalary;
InBlock.gif
InBlock.gif        
public Employee()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            intEID 
= 0;
InBlock.gif            decimalSalary 
= 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [JoinedKey(
"ID")]   //JoinedKey代替PrimaryKey的位置
InBlock.gif
        public int EID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return intEID;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 薪水
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Property]
InBlock.gif        
public decimal Salary
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return decimalSalary;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

四、编写调用代码(只列出添加Employee的代码)
None.gif private   void  button2_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IConfigurationSource source 
= System.Configuration.ConfigurationManager.GetSection("activerecord"as IConfigurationSource;
InBlock.gif    ActiveRecordStarter.Initialize(source, 
typeof(Inherit.Employee), typeof(Inherit.User));
InBlock.gif
InBlock.gif    
//使用事务处理
InBlock.gif
    using (TransactionScope tran = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Inherit.Employee objEmployee 
= new Inherit.Employee();
InBlock.gif
InBlock.gif        objEmployee.Name 
= "jailu";
InBlock.gif        objEmployee.Password 
= "123456789";
InBlock.gif        objEmployee.Salary 
= 1000;
InBlock.gif
InBlock.gif        objEmployee.Save();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值