来源:http://www.cnblogs.com/zxj159/p/4082987.html
配置数据库驱动:
Model层引用:Castle.ActiveRecord.dll、NHibernate.dll
Web层引用:Model、Castle.ActiveRecord-3.0.RC中的所有DLL
Web中新增NHibernate.config配置文件:
<?xml version="1.0" encoding="utf-8"?> <activerecord isWeb="true"> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> <add key="connection.connection_string" value="UID=sa;Password=123456;Initial Catalog=ActiveRecord_Blog;Data Source=." /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" /> </config> </activerecord>
Global.asax:加载配置文件
protected void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 //AuthConfig.RegisterOpenAuth(); //RouteConfig.RegisterRoutes(RouteTable.Routes); InitActiveRecord(); } //Castle Record Register Model private void InitActiveRecord() { try { //加载配置文件 string NHibernateFilePath = Server.MapPath("~/NHibernate.config"); XmlConfigurationSource source = new XmlConfigurationSource(NHibernateFilePath); //注册数据模型 ActiveRecordStarter.Initialize(source, typeof(Models.UserInfo), typeof(Models.ThemeInfo), typeof(Models.CommentInfo), typeof(Models.CategoryInfo)); } catch (Exception) { throw; } }