使用Sybase asa9使用命名参数进行nhibernate
本文关键字:参数 nhibernate Sybase asa9 使用 | 更新日期: 2023-09-27 18:18:46
试图让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=?]
您需要创建一个自定义的连接驱动程序,因为默认的连接驱动程序使用错误的设置。升级到新版本的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 ":"; }
}
}