NHibernate Dictionary:无法初始化集合

时间:2012-02-20 09:58:10

标签: c# sqlite nhibernate dictionary

我班上有一本字典

public class AffectedVehicleInScenario : DomainObjectWithId {

    ... some other properties ...

    private readonly int _id;
    private readonly IDictionary<int, int> _installationRates
        = new Dictionary<int, int>();

    ... some code ...

}

并将其映射如下

<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>

插入工作完美,但从数据库加载(SQLITE)时,我得到一个例外:

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.

有人可以帮帮我吗?映射不正确还是nhibernate中的错误?

提前致谢

托比

更新: Created表如下:

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
)

1 个答案:

答案 0 :(得分:2)

我想这是因为sqlite驱动程序返回一个短框作为对象,不能直接转换为int。您可以使用IDictionary<int, short>或使用工具IUserType,或者可能有一些配置告诉NH不同。