试图让 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=?]