c# - NHibernate 字典 : Could not initialize a collection

标签 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 中的错误?

提前致谢

托比

更新:
创建的表如下:
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
)

最佳答案

我猜这是因为 sqlite 驱动程序返回一个不能直接转换为 int 的短框对象。你可以使用 IDictionary<int, short>或者使用实现 IUserType 或者可能有一些配置来告诉 NH 的区别。

关于c# - NHibernate 字典 : Could not initialize a collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359196/

相关文章:

c# - 返回数据时出现 WCF CommunicationException

c# - 回发后清除隐藏的输入字段

c# - 使用表创建 c# 类。 Sqlite网

sqlite - SQLite:创建表时使用x'abc'语法

java - 无法创建数据库

c# - 取 LINQ 中每个分组项目的 Top(X)

nhibernate - 使用 Fluent NHibernate 过滤软删除数据

linq - NHibernate - LINQ 限制

c# - 如何在 ASP.NET Core 中获取 TLS/SSL 相关信息?

c# - asp.net webapi : how to pass optional parameters?