I have a dictionary in my class
public class AffectedVehicleInScenario : DomainObjectWithId {
... some other properties ...
private readonly int _id;
private readonly IDictionary<int, int> _installationRates
= new Dictionary<int, int>();
... some code ...
}
and mapped it as follows
<class name="AffectedVehicleInScenario"
table="tblAffectedVehicleInScenario"
lazy="false">
<id name="_id"
column="Id"
access="field">
<generator class="native" />
</id>
... some other properties ...
<map name="_installationRates"
table="tblInstallationRatesOfAffectedVehicleInScenario"
access="field"
lazy="false">
<key column="AffectedVehicleInScenarioId" not-null="true"/>
<index column="Year" type="Int32"/>
<element column="InstallationRate"
not-null="true"
type="Int32"/>
</map>
Inserting works perfect but when loading from database (SQLITE) I get an exception:
Spring.Data.NHibernate.HibernateSystemException: could not initialize a collection:
[Com.QueoMedia.CO2Simulationstool.Domain.AffectedVehicleInScenario._installationRates#1][SQL: SELECT installat0_.AffectedVehicleInScenarioId as Affected1_0_, installat0_.InstallationRate as Installa2_0_, installat0_.Year as Year0_ FROM tblInstallationRatesOfAffectedVehicleInScenario installat0_ WHERE installat0_.AffectedVehicleInScenarioId=?]
---> NHibernate.Exceptions.GenericADOException: could not initialize a collection:
[Com.QueoMedia.CO2Simulationstool.Domain.AffectedVehicleInScenario._installationRates#1][SQL: SELECT installat0_.AffectedVehicleInScenarioId as Affected1_0_, installat0_.InstallationRate as Installa2_0_, installat0_.Year as Year0_ FROM tblInstallationRatesOfAffectedVehicleInScenario installat0_ WHERE installat0_.AffectedVehicleInScenarioId=?]
---> System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
Could someone help me out please? Is the mapping incorrect or is it a bug in nhibernate?
Thanks in advance
Tobi
Update:
The Created table is the following:
CREATE TABLE tblInstallationRatesOfAffectedVehicleInScenario
(
AffectedVehicleInScenarioId INT not null,
InstallationRate SMALLINT not null,
Year INT not null,
primary key (AffectedVehicleInScenarioId, Year),
constraint FKDF60481555A07D7C foreign key (AffectedVehicleInScenarioId) references tblAffectedVehicleInScenario
)
i guess it is because sqlite driver returns a short boxed as object which is not directly convertable to int. you could use
IDictionary<int, short>or use implement IUserType or there maybe some config to tell NH the difference.