Have started to use NHibernate on sybase ASE data, problem am facing is when I load entity I ge开发者_JAVA技巧t below error
"System.IndexOutOfRangeException : Invalid index 0 for this OdbcParameterCollection with Count=0."
This is how I configure session
properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
properties["connection.driver_class"] = "NHibernate.Driver.OdbcDriver";
properties["connection.connection_string"] = @"Driver={Adaptive Server Enterprise};server=;port=; db=;uid=;pwd=";
properties["dialect"] = "NHibernate.Dialect.SybaseASE15Dialect";
And object mapping
<class name="MenuGroup" table="MENU_GROUP">
<id name="Id" column="id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="name" type="String" length="100" not-null="true" />
<property name="Position" column="position" type="Int32" />
</class>
and If I do
var menuGroup = _session.Get<Menu.MenuGroup>(1);
I get error
NHibernate.Exceptions.GenericADOException : could not load an entity: [DomainModel.Menu.MenuGroup#1][SQL: SELECT menugroup0_.id as id1_0_, menugroup0_.name as name1_0_, menugroup0_.position as position1_0_ FROM MENU_GROUP menugroup0_ WHERE menugroup0_.id=?] ----> System.IndexOutOfRangeException : Invalid index 0 for this OdbcParameterCollection with Count=0.
I solved this problem by creating my own connection driver
using NHibernate.Driver;
namespace Framework.Persistency
{
public sealed class MySybaseSQLAnywhereDriver : SybaseSQLAnywhereDriver
{
public override bool UseNamedPrefixInSql
{
//default is false
get { return true; }
}
public override bool UseNamedPrefixInParameter
{
//default is false
get { return true; }
}
public override string NamedPrefix
{
//default is string.Empty
get { return ":"; }
}
}
}
And use it in the NHibernate config:
configDictionary.Add(Environment.ConnectionDriver, typeof(MySybaseSQLAnywhereDriver).AssemblyQualifiedName);
moving away from odbc helped, changed the config to
properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider"; properties["connection.driver_class"] = "NHibernate.Driver.SybaseAseClientDriver"; properties["connection.connection_string"] = @"server=*;port=5000; db=;user id=*;password=;"; properties["dialect"] = "NHibernate.Dialect.SybaseASE15Dialect";
精彩评论