(一)http://www.castleproject.org/castle/download.html下载ActiveRecord 2.0
(二)独立的配置文件MsSqlConfigurationSource.xml
<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=selftest;UID=sa;Password=123" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
</config>
</activerecord>
(三)添加引用
(四)Start
Castle.ActiveRecord.Framework.Config.XmlConfigurationSource source = new Castle.ActiveRecord.Framework.Config.XmlConfigurationSource(Server.MapPath("MsSqlConfigurationSource.xml"));
Castle.ActiveRecord.ActiveRecordStarter.Initialize(source, typeof(Customer));
(五)实体类(从泛型基类派生)
using System.Collections;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
[ActiveRecord("Customer")]
public class Customer : ActiveRecordBase<Customer>
{
public Customer()
{
}
[PrimaryKey(PrimaryKeyType.Assigned,"customerid")]
public int Unid { get; set; }
[Property("FirstName")]
public string FirstName { get; set; }
[Property("LastName")]
public string LastName { get; set; }
public static IList FindAll()
{
return (IList)FindAll(typeof(Customer));
}
public static IList<Customer> ShowList()
{
SimpleQuery<Customer> q = new SimpleQuery<Customer>(@"from Customer");
return q.Execute();
}
}
(六)测试