1、ActiveRecord配置文件ARConfig.xml,并将配置文件的“生成操作”改成“嵌入的资源”
<?xml version="1.0" encoding="utf-8" ?>
<activerecord isWeb="true">
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SQLite20Driver" />
<add key="dialect" value="NHibernate.Dialect.SQLiteDialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="data source=|DataDirectory|message.db;Version=3;" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="show_sql" value="true"/>
</config>
</activerecord>
<activerecord isWeb="true">
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SQLite20Driver" />
<add key="dialect" value="NHibernate.Dialect.SQLiteDialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="data source=|DataDirectory|message.db;Version=3;" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="show_sql" value="true"/>
</config>
</activerecord>
2、使用到的DLL文件
3、测试实体类UserInfo
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using NHibernate;
namespace CY.Domain
{
[ActiveRecord("T_User")]
public class UserInfo : ActiveRecordBase
{
#region 属性
[PrimaryKey(PrimaryKeyType.Identity, "ID")]
public int ID { get; set; }
[Property("UserName")]
public string Name { get; set; }
[Property("Password")]
public string Password { get; set; }
[Property("RegTime")]
public DateTime RegTime { get; set; }
#endregion
#region 方法
public static void Insert(UserInfo user)
{
Save(user);
}
public static IList<UserInfo> Select()
{
return (IList<UserInfo>)FindAll(typeof(UserInfo));
}
public static UserInfo Query(int id)
{
object model = FindByPrimaryKey(typeof(UserInfo), id);
return model != null ? model as UserInfo : null;
}
public static void Del(int id)
{
UserInfo model = Query(id);
if (model != null)
{
Delete(model);
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using NHibernate;
namespace CY.Domain
{
[ActiveRecord("T_User")]
public class UserInfo : ActiveRecordBase
{
#region 属性
[PrimaryKey(PrimaryKeyType.Identity, "ID")]
public int ID { get; set; }
[Property("UserName")]
public string Name { get; set; }
[Property("Password")]
public string Password { get; set; }
[Property("RegTime")]
public DateTime RegTime { get; set; }
#endregion
#region 方法
public static void Insert(UserInfo user)
{
Save(user);
}
public static IList<UserInfo> Select()
{
return (IList<UserInfo>)FindAll(typeof(UserInfo));
}
public static UserInfo Query(int id)
{
object model = FindByPrimaryKey(typeof(UserInfo), id);
return model != null ? model as UserInfo : null;
}
public static void Del(int id)
{
UserInfo model = Query(id);
if (model != null)
{
Delete(model);
}
}
#endregion
}
}
4、初始化实体类对象(Global.asax)
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("ARConfig.xml"));
ActiveRecordStarter.Initialize(source, typeof(UserInfo));
}
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("ARConfig.xml"));
ActiveRecordStarter.Initialize(source, typeof(UserInfo));
}
5、使用
for (int i = 0; i < 10; i++)
{
UserInfo.Insert(new UserInfo()
{
Name = "user" + i.ToString(),
Password = "123456",
RegTime = DateTime.Now
});
}
IList<UserInfo> models = UserInfo.Select();
ViewData["users"] = models;
{
UserInfo.Insert(new UserInfo()
{
Name = "user" + i.ToString(),
Password = "123456",
RegTime = DateTime.Now
});
}
IList<UserInfo> models = UserInfo.Select();
ViewData["users"] = models;