最近搞个项目,因为以前一直用AR,所以就继续用,但是这回要分数据库,不再是一个数据库了,麻烦就来了。
网上扒拉了几天,发现千篇一律,不知道是谁抄袭谁的,而且只是理论上的,没有实践,因为我实践了,没有成功的,全部是这样的配置文件
<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=localhost;Initial Catalog=Test;UID=sa;Password=sa" />
</config>
<config type="ConsoleApplication1.CastleActiveRecord.Test1Base , Learn.CUI">
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=localhost;Initial Catalog=Test1;UID=sa;Password=sa" />
</config>
<config type="ConsoleApplication1.CastleActiveRecord.Test2Base , Learn.CUI">
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=localhost;Initial Catalog=Test2;UID=sa;Password=sa" />
</config>
</activerecord>
试了一下,一直显示 Could not obtain type from name xxx 错误, 郁闷啊,真是日了狗了,这么多人只管转,不管使用吗?
最后只有百度了,百度了大半天不行,最后Bing了一下,哎,你还别说,Bing真是好东西,发现了有人和我一样的困惑,
原文地址:http://blog.csdn.net/easeyeah/article/details/6600613 http://blog.csdn.net/educast/article/details/6290707
按照原文依葫芦画瓢,找Castle源文件,修改,编译,废了大半晚上的时间(程序猿真苦逼),折腾了大半晚上,发现及时填写了type和dll通过反射,还是加载的默认数据配置,X你妹啊,怎么回事?于是立即发帖,求助,100分(白给都没人接分),又给博客的博主发私信,恨不得叫爷,最后在今晚终于研究成果!噎!喜大普奔!别烦我,我想静静!噎别问我静静是谁!
只需注意三件事:
1、抽象类abstract class必须继承ActiveRecord Base
2、新写的类必须要继承一个抽象类abstract class
3、必须初始化抽象基类以及新的类,我就是缺了这一步才导致的。而且在配置文件的type中一定要填写抽象类,而不是实体类。
需要编译好的ActiveRecord的朋友留下邮箱,私信我,看到给你发!
我的代码部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.ActiveRecord;
<span style="color:#333333;">
[ActiveRecord()]
public abstract class </span><span style="color:#ff0000;">Base</span><span style="color:#333333;"> : ActiveRecordBase
{
}
/// <summary>
///实体类
/// </summary>
[ActiveRecord("T_MlOrder")]
public class MlOrder : </span><span style="color:#ff0000;">Base</span><span style="color:#333333;">
{//下面定义的是字段</span>
}
配置文件
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.connection.connection_string" value="Data Source=server03;Initial Catalog=test;User ID=sa;Password=a;connection timeout=500 "/>
<add key="hibernate.show_sql" value="true"/>
</config>
<config type="Base" dll="ZYERP.MES.MLOrder.dll">
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.connection.connection_string" value="Data Source=server03;Initial Catalog=test1;User ID=sa;Password=a;connection timeout=500 "/>
</config>
</activerecord>