摘要:本文详细介绍了ActiveRecord中的基本映射,对于关联映射会在后续文章中通过一些具体的实例来说明。
主要内容
简单映射
1.ActiveRecordAttribute
2. PrimaryKeyAttribute
3.CompositeKeyAttribute
4.PropertyAttribute
5.FieldAttribute
一.ActiveRecordAttribute
每一个实体类都必须继承于基类ActiveRecordBase,并在实体类上设置特性ActiveRecordAttribute,示例代码
//指定数据库表名

[ActiveRecord("Blogs")]

public class Blog : ActiveRecordBase



{

//

}

//不指定数据库表名

[ActiveRecord]

public class Blog : ActiveRecordBase



{

//

}
ActiveRecordAttribute说明
|
属性
|
说明
|
示例
|
|
Table
|
指定持久化类所对应的数据库表名,如果表名与类名相同,可以省略
|
[ActiveRecord("Blogs")]
[ActiveRecord(Table="Blogs")]
|
|
Schema
|
指定Schema的名字
|
Schema="ARDemo"
|
|
Proxy
|
指定一个接口,在延迟装载时作为代理使用
|
|
|
DiscriminatorColumn
|
识别器的字段名
|
DiscriminatorColumn="Blog"
|
|
DiscriminatorType
|
识别器的字段类型
|
DiscriminatorType="String"
|
|
DiscriminatorValue
|
识别器字段的值
|
|
|
Where
|
指定一个附加SQL的Where子句
|
Where="IsPost = 0"
|
|
Lazy
|
指定是否延迟加载
|
Lazy=true|false
|
二.PrimaryKeyAttribute
在实体类中,通过PrimaryKeyAttribute来指定表的主键,示例代码
//指定主键字段名

[ActiveRecord()]

public class Blog : ActiveRecordBase



{

private int id;


[PrimaryKey("blog_id")]

public int Id


{


get
{ return id; }


set
{ id = value; }

}

}

//不指定主键字段名

[ActiveRecord()]

public class Blog : ActiveRecordBase



{

private int id;


[PrimaryKey]

public int Id


{


get
{ return id; }


set
{ id = value; }

}

}

PrimaryKeyAttribute说明
|
属性
|
说明
|
示例
|
|
PrimaryKeyType
|
主键生成的方式,如果不指定,则默认的方式为PrimaryKeyType.Native
|
PrimaryKeyType.Native
|
|
Column
|
主键字段名称,如果跟属性名相同,可以不用指定
|
PrimaryKey("blog_id")
|
|
ColumnType
|
主键字段的类型
|
|
|
Generator
|
是一个.NET类的名字,用来为该持久化类的实例生成唯一的标识。
|
|
|
Params
|
用Params来提供Generator所需要的配置参数或初始化参数
|
|
|
Length
|
主键字段的长度
|
Length=10
|
|
SequenceName
|
当指定主键的生成方式为Sequence时,序列的名称
|
PrimaryKey(PrimaryKeyType.Sequence, SequenceName="myseqname")
|
|
UnsavedValue
|
用来标志该实例是刚刚创建的,尚未保存。
|
|
主键的生成方式介绍
|
名称
|
说明
|
|
Identity
|
对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持,生成自增的整型
|
|
Sequence
|
序列,对DB2,MySQL, PostgreSQL, Oracle的内置标识字段提供支持,生成自增的整型。
|
|
HiLo
|
高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符。
|
|
SeqHiLo
|
使用序列的高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符,给定一个数据库序列(sequence)的名字。
|
|
UuidHex
|
用一个System.Guid和它的ToString(string format)方法生成字符串类型的标识符。
|
|
UuidString
|
用一个新的System.Guid产生一个byte[] ,把它转换成字符串。
|
|
Guid
|
用一个新的System.Guid 作为标识符。
|
|
GuidComb
|
用Jimmy Nilsso的一个算法产生一个新的System.Guid。
|
|
Native
|
根据底层数据库的能力选择 identity, sequence 或者 hilo中的一个。默认值。
|
|
Assigned
|
让应用程序在自己为对象分配一个标示符。
|
|
Foreign
|
使用另外一个相关联的对象的标识符。
|
三.CompositeKeyAttribute
如果使用组合键,需要我们自定义一个类来作为主键属性的类型。示例代码
[PrimaryKey]

public MyCompositeKey ID



{


get
{ return _key; }


set
{ _key = value; }

}
对于组合键类,除了需要加上CompositeKey特性之外,它还需要是可序列化的,并且要求实现Equals和GetHashCode方法。ActiveRecord官方网站上提供的一个组合键的示例程序如下:
[CompositeKey, Serializable]

public class MyCompositeKey



{

private string _keyA;

private string _keyB;


[KeyProperty]

public virtual string KeyA


{


get
{ return _keyA; }


set
{ _keyA = value; }

}


[KeyProperty]

public virtual string KeyB


{


get
{ return _keyB; }


set
{ _keyB = value; }

}


public override string ToString()


{


return string.Join( ":", new string[]
{ _keyA, _keyB } );

}


public override bool Equals( object obj )


{

if( obj == this ) return true;

if( obj == null || obj.GetType() != this.GetType() ) return false;

MyCompositeKey test = ( MyCompositeKey ) obj;

return ( _keyA == test.KeyA || (_keyA != null && _keyA.Equals( test.KeyA ) ) ) &&

( _keyB == test.KeyB || ( _keyB != null && _keyB.Equals( test.KeyB ) ) );

}


public override int GetHashCode()


{

return _keyA.GetHashCode() ^ _keyB.GetHashCode();

}

}
四.PropertyAttribute
在ActiveRecord中通过PropertyAttribute来指定实体类属性与数据库中的字段映射。
[ActiveRecord()]

public class Blog : ActiveRecordBase



{

//不指定字段名

[Property]

public int Name


{


get
{ return _name; }


set
{ _name = value; }

}

}


[ActiveRecord()]

public class Blog : ActiveRecordBase



{

//指定字段名

[Property("blog_name")]

public int Name


{


get
{ return _name; }

![]()