Active Record学习笔记(二):处理One-To-One映射

上一篇学习笔记: ActiveRecord学习笔记(一):初步接触

这篇学习笔记主要介绍了ActiveRecord如何处理One-To-One映射。本文涉及两个实体类User(用户)和NativePalce(祖籍),两个类是一对一的关系:


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

一、编写数据库脚本
None.gif -- User类对应的数据表
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
-- NativePlace类对应的数据表
None.gif
Create   Table   [ NativePlace ]
None.gif(
None.gif    ID 
int   Primary   Key ,
None.gif    City 
Varchar ( 50 ),
None.gif    Province 
Varchar ( 50 )
None.gif)

  在编写数据库时需要注意的是:
  1.副表NativePalce的主键不能为自增类型;
  2.主表Users和副表NativePalce的主键名必须一致。


二、OneToOne属性说明:
  在Castle.ActiveRecord中,用OneToOne属性代替NHibernate配置文件中的<OneToOne></OneToOne>标签。具有以下子属性:
  a.cascade:(可选) 表明操作是否从父对象级联到被关联的对象
  b.constrained:(可选) 表明该类对应的表对应的数据库表,和被关联的对象所对应的数据库表之间,通过一个外键引用对主键进行约束。这个选项影响 Save()Delete()在级联执行时的先后顺序(也在schema export tool中被使用)
  c.outer-join:(可选 - 默认为 auto):当设置 hibernate.use_outer_join的时候,对这个关联允许外连接抓取
  d.access:(可选 - defaults to property): 用来访问属性的策略
  e.CustomAccess:还不知道干嘛滴

三、编写实体类
  本文使用主键关联处理一对一的关系。

  主键关联不需要额外的表字段;两行是通过这种一对一关系相关联的,那么这两行就共享同样的主关键字值。所以如果你希望两个对象通过主键一对一关联,你必须确认它们被赋予同样的标识值!

  1.在上篇文章中的User类中增加属性NativePlace和字段objNativePlace
None.gif public   class  User : ActiveRecordBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//dot.gif
InBlock.gif
    private NativePlace objNativePlace;
InBlock.gif
InBlock.gif    
//dot.gif
InBlock.gif
    [OneToOne(Cascade = CascadeEnum.All)]
InBlock.gif    
public NativePlace NativePlace
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (objNativePlace == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                objNativePlace 
= new NativePlace();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return objNativePlace;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
  2.编写实体类NativePlace
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 祖籍信息
ExpandedBlockEnd.gif
/// </summary>

None.gif [ActiveRecord]   // 类名与数据表名一样,也可以不写明映射的数据表名
None.gif
public   class  NativePlace : ActiveRecordBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int intID;
InBlock.gif    
private string strCity;
InBlock.gif    
private string strProvince;
InBlock.gif
InBlock.gif    
private User objUser;
InBlock.gif
InBlock.gif    
//注意此处的主键类型
InBlock.gif
    [PrimaryKey(PrimaryKeyType.Foreign)]
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]
InBlock.gif    
public string Province
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return strProvince;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 城市
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Property]
InBlock.gif    
public string City
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return strCity;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [OneToOne]
InBlock.gif    
public User User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (objUser == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                objUser 
= new User();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return objUser;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

  3.配置文件:以上篇文章的一样

四、编写表示层调用代码(只列出新增User的过程):
None.gif private   void  btnAddNewUser_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IConfigurationSource source 
= System.Configuration.ConfigurationSettings.GetConfig("activerecord"as IConfigurationSource;
InBlock.gif    ActiveRecordStarter.Initialize(source, 
typeof(User), typeof(NativePlace));
InBlock.gif
InBlock.gif    
//使用事务处理
InBlock.gif
    using (TransactionScope tran = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            User objUser 
= new User();
InBlock.gif            objUser.Name 
= "jailu";
InBlock.gif            objUser.Password 
= "123456789";
InBlock.gif
InBlock.gif            NativePlace objNativePlace 
= new NativePlace();
InBlock.gif            objNativePlace.City 
= "LongYan";
InBlock.gif            objNativePlace.Province 
= "FuJian";
InBlock.gif
InBlock.gif            
//指出对应关系
InBlock.gif
            objUser.NativePlace = objNativePlace;
InBlock.gif            objNativePlace.User 
= objUser;
InBlock.gif
InBlock.gif            objUser.Save(); 
//保存objUser对象
InBlock.gif

InBlock.gif            tran.VoteCommit();  
//执行事务
InBlock.gif

InBlock.gif            MessageBox.Show(
"Success!");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tran.VoteRollBack();    
//若出现异常,回滚事务
InBlock.gif
            MessageBox.Show("Fail!" + ex.Message);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<br> Active Audio Record ActiveX控件 可以直接录制声音为MP3、WAV、wma 、ogg、au、aiff 和vox格式的音频文件。您可以通过设置内码参量完全控制音像文件的质量。<br><br> Active Audio Record ActiveX控件 支持标准声卡,还支持多张声卡并且具有多条设备线。您能容易地记录任一个来源的声音:譬如在网上流出的音像、收音机、音像球员(即传媒播放装置、WinAmp, 真正的球员等), midi 、话筒、CD 、磁带、LPs等。<br><br> Active Audio Record ActiveX控件 兼容许多支持ActiveX的语言,譬如:Visual C++, Visual Basic, Delphi, C++ Builder, .Net languages like C#, VB.Net, Java, Scripts like Perl, Php, Python, ASP.Net。<br><br> Active Audio Record ActiveX控件的特点: 对wav, MP3 、 wma 、ogg 、vox 、au 、aiff 、mp4 和flac格式音像直接纪录,若正在进行格式化则不创建临时文件; 支持多条声卡和混频线路; 为混频线路设置容量级别; 在录音期间静音探察; 得到音像频道音量值; 支持wav、wma 和MP3; 支持ID3 标记、MP3 和WMA 标记、OGG和FLAC 标记; 提供VB、VBScript、C#、Perl、JScript、VB.Net、Delphi实例。 Active Audio Record ActiveX控件一些组件的功能: LONG GetDeviceCount:得到声音设备的数目; LONG GetDeviceName(LONG idx):得到声音设备的名字; LONG DeviceIndex:获取/设置当前的声音录音设备; SetOutputFileName(String strFile) :设置输出文件的名; StartRecord:以录音参量开始录音,将这些参量传送到声卡; StopRecord:停止录音; LONG GetDeviceLineCount:得到混频线路数量; LONG DeviceIndex: 获取/设置当前的设备目录; LONG DeviceLineVolume:获取/设置当前的混频线路,在0 和100 之间; GetDeviceLineName(LONG idx) :得到混频线路名称; LONG GetRecordTime :在几秒钟内得到记录时间; LONG GetLeftInputLevel:得到左声道输入容限,在0 和32768 之间; LONG LONG GetRightInputLevel:得到右声道输入容限,在0 和32768 之间; LONG LONG SetSilenceLevel:得到当前的门限值; LONG LONG SetDetectSilence :设置默认的容量水平,在0 和32768 之间; LONG LONG FileFormat :设置文件格式; LONG SetCode(String strCode) :设置注册代码(试用版本)。 Active Audio Record ActiveX控件V 2.0的系统需求: Windows 98 以上操作系统,声卡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值