2012-02-20 56 views
1

我有一本字典在我的課NHibernate的解釋:無法初始化集合

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中的錯誤?

在此先感謝

託比

更新: 創建的表如下:

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 
) 
+0

看起來像列類型不是整數,以我 – Firo 2012-02-20 10:29:06

+0

@Firo:我添加腳本用於創建使用表 – Tobias 2012-02-20 10:35:27

回答

2

我想這是因爲sqlite的驅動程序返回一個短盒裝爲對象,不能直接使用可轉換爲int。你可以使用IDictionary<int, short>或使用實現IUserType或者可能有一些配置告訴NH的區別。

+0

短嘗試過,但得到了同樣的錯誤 – Tobias 2012-02-20 11:21:11

+0

你改爲'<元素列=「InstallationRate」 not-null =「true」/>'(讓NHibernate猜測類型)? – Firo 2012-02-20 12:53:37

+0

如果我省略元素的類型定義(字典的值),它可以正常工作。非常感謝。 – Tobias 2012-02-20 13:07:57

相關問題