Model中的Demo:
using Castle.ActiveRecord; using Castle.ActiveRecord.Queries; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { //指定数据表,Lazy为延迟加载,一旦指定为延迟加载,其属性应该加上virtual修饰。还有其他特性参数,自己慢慢琢磨 //继承ActiveRecordBase<T> 基类中含有默认实现的方法 [ActiveRecord("CategoryInfo", Lazy = true)] public class CategoryInfo : ActiveRecordBase<CategoryInfo> { // 指定数据表中的主键,指定ID是自增列
//如果属性名和字段名一致,[Property()]中可以为空,也可以写上字段的名字。 [PrimaryKey(PrimaryKeyType.Identity, "ID")] public virtual int ID { get; set; } // 指定数据表中的列,[Property("Title")] 中的Title是数据库中列名称 [Property("Title")] public virtual string Title { get; set; } [Property("Description")] public virtual string Description { get; set; } [Property("CreateDate")] public virtual DateTime CreateDate { get; set; } //是否推荐 1:是 0:否 [Property("Type")] public virtual int Type { get; set; } //多对多 //typeof(ThemeInfo):对方表的实体类,Table:关联中间表,ColumnRef:关联中间表中与对方实体相关的列,ColumnKey:关联中间表中与本实体相关的列, //Lazy:延迟加载,通过本实体获取对方实体信息时,才会去数据库查询 //Cascade:级联操作,标记何种操作会级联到子表 [HasAndBelongsToMany(typeof(ThemeInfo), Table = "CategoryThemeMapping", ColumnRef = "ThemeID", ColumnKey = "CategoryID", Lazy = true, Cascade = ManyRelationCascadeEnum.Delete)] public virtual IList<ThemeInfo> ThemeInfos { get; set; }
//查询所有推荐的分类 public static IList<CategoryInfo> FindAllForTopCategory() { //TODO:语法研究 SimpleQuery<CategoryInfo> query = new SimpleQuery<CategoryInfo>(@" from CategoryInfo c where c.Type=1 "); return query.Execute(); } /// <summary> /// 依据主键ID查询一个实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public static CategoryInfo Find(int id) { return FindByPrimaryKey(id); } } }
//多对一,对应ThemeInfo的Comments属性,Column = "ThemeID" ThemeID是本类中的外键字段.Insert=false,Update=false:当从表单独更新的时候会出现错误,加入此句避免。 //[BelongsTo(Column = "ThemeID", Insert = false, Update = false)] //public virtual ThemeInfo Theme { get; set; }
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说明
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:使用另外一个相关联的对象的标识符。
如果使用组合键,需要我们自定义一个类来作为主键属性的类型。对于组合键类,除了需要加上CompositeKey特性之外,它还需要是可序列化的,并且要求实现Equals和GetHashCode方法。
示例代码:
[ActiveRecord("MessageInfo")] public class MessageInfo : ActiveRecordBase<MessageInfo> { [PrimaryKey] public MyCompositeKey ID { get; set; } } [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(); } }
在ActiveRecord中通过PropertyAttribute来指定实体类属性与数据库中的字段映射。
PropertyAttribute说明
Column:对应的数据库字段名。例:Property("blog_name")
ColumnType:对应的字段类型
Formula:一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。
UnsavedValue:用来标志该实例是刚刚创建的,尚未保存。
Length:字段的长度。例:Length=10
NotNull:是否可以为空。例:NotNull=true|false
Unique:是否允许重复。例:Unique=true|false
Update:表明在用于UPDATE 的SQL语句中是否包含这个字段。默认为true。例:Update=true|false
Insert:表明在用于INSERT的SQL语句中是否包含这个字段。默认为true。例:Insert=true|false
在ActiveRecord中,允许我们直接对Field进行映射,使用FieldAttribute
FieldAttribute说明
Column:对应的数据库字段名。Property("blog_name")
ColumnType:对应的字段类型。
Formula:一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。
UnsavedValue:用来标志该实例是刚刚创建的,尚未保存。
Length:字段的长度。Length=10
NotNull:是否可以为空。NotNull=true|false
Unique:是否允许重复。Unique=true|false
Update:表明在用于UPDATE 的SQL语句中是否包含这个字段。默认为true。Update=true|false
Insert:表明在用于INSERT的SQL语句中是否包含这个字段。默认为true。Insert=true|false
NestedAttribute:在映射的时候我们也可以用子对象来映射数据库中的字段,示例代码
[ActiveRecord("CompanyInfo")] public class CompanyInfo : ActiveRecordBase<CompanyInfo> { private PostalAddress _address; [Nested] public PostalAddress Address { get { return _address; } set { _address = value; } } } public class PostalAddress { private String _address; private String _city; private String _state; private String _zipcode; public PostalAddress() { } public PostalAddress(String address, String city, String state, String zipcode) { _address = address; _city = city; _state = state; _zipcode = zipcode; } [Property] public String Address { get { return _address; } set { _address = value; } } [Property] public String City { get { return _city; } set { _city = value; } } [Property] public String State { get { return _state; } set { _state = value; } } [Property] public String ZipCode { get { return _zipcode; } set { _zipcode = value; } } }
NestedAttribute说明
Update:表明在用于UPDATE 的SQL语句中是否包含这个字段。默认为true。Update=true|false
Insert:表明在用于INSERT的SQL语句中是否包含这个字段。默认为true。Insert=true|false
来源:http://www.cnblogs.com/Terrylee/archive/2006/04/06/367978.html