尝试让NHibernate与Sybase ASA9一起使用,并且在命名参数出错时抛出错误:
Invalid index 0 for this AsaParameterCollection with Count=0.
我尝试过使用NHibernate ODBC驱动程序和当前的ASA驱动程序。我可以直接查询没有参数,所以我知道连接正在发生。看来这个参数没有正确传递给ASA,但我一直在搞清楚我是做错了什么,或者这只是NH和ASA的一个问题。为了达到这个目的,我们有点头疼,也许这也将有助于未来的某些人。该值是正确的,并且在生成的sql的最后一个片段中可见,并且查询以等于问号结束,这似乎是有效的Sybase,只是不清楚它在哪里发生故障。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.SybaseASA9Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SybaseAsaClientDriver</property>
<property name="connection.connection_string">Uid=;Pwd=;Dsn=</property>
<property name="hbm2ddl.keywords">none</property><!--could cause problems? update needed when going to sybase driver instead of odbc-->
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="domain.table.hbm.xml" assembly="Label3700" />-->
</session-factory>
</hibernate-configuration>
映射
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Label3700"
namespace="Label3700.Domain">
<class name="menu_item" table="micros.mi_def">
<id name="mi_seq">
<column name="mi_seq" sql-type="integer" not-null="true"/>
<generator class="identity" />
</id>
<property name="obj_num" />
<property name="name_1" />
<property name="fam_grp_seq" />
</class>
</hibernate-mapping>
类
public class menu_item
{
public virtual int mi_seq { get; set; }
public virtual int obj_num { get; set; }
public virtual string name_1 { get; set; }
public virtual int fam_grp_seq { get; set; }
}
查询
IList<menu_item> mi = session.CreateQuery("FROM menu_item m where m.fam_grp_seq = :famGrpSeq")
.SetParameter("famGrpSeq", familyGroupSeq)
.List<menu_item>();
错误
could not execute query
[ select menu_item0_.mi_seq as mi1_1_, menu_item0_.obj_num as obj2_1_,menu_item0_.name_1 as name3_1_, menu_item0_.fam_grp_seq as fam4_1_ from micros.mi_def menu_item0_ where menu_item0_.fam_grp_seq=? ]
Name:famGrpSeq - Value:44
[SQL: select menu_item0_.mi_seq as mi1_1_, menu_item0_.obj_num as obj2_1_,menu_item0_.name_1 as name3_1_, menu_item0_.fam_grp_seq as fam4_1_ from micros.mi_def menu_item0_ where menu_item0_.fam_grp_seq=?]
答案 0 :(得分:0)
您需要创建自定义连接驱动程序,因为默认连接驱动程序使用了错误的设置。升级到更新版本的NHibernate可能会消除这个问题。
在我的sybase连接驱动程序中,我已经覆盖了这些属性:
public class SqlAnywhereConnectionDriver : ReflectionBasedDriver
{
//other code removed…
public override bool UseNamedPrefixInSql
{
get { return true; }
}
public override bool UseNamedPrefixInParameter
{
get { return true; }
}
public override string NamedPrefix
{
get { return ":"; }
}
}