【问题标题】:NHibernate mapping index out of rangeNHibernate 映射索引超出范围
【发布时间】:2014-06-18 06:35:38
【问题描述】:

我有两个班级:

 public class CarModel
    {
        public virtual int Id { get; set; }
        public virtual string model_name { get; set; }
    }

  public class Transport
    {
          public virtual int Id { get; set; } 
          public virtual string lic_plate { get; set; } 
          public virtual int model { get; set; } 
          public virtual string odometer { get; set; }
          public virtual string moto_val { get; set; } 
          public virtual Class.CarModel Modelis { get; set; }    
    }

和映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="web_nt" namespace="web_nt.Models">

  <class name="Transport" table="transport" dynamic-update="true" lazy="false">
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="lic_plate" />
    <property name="model" />
    <property name="odometer" />
    <property name="moto_val" />
    <many-to-one name="Modelis" column="model"  cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 
  </class>

  <class name="web_nt.Models.Class.CarModel" table="car_model">
    <id name="Id" column="id" type="int">
      <generator class="native" />
    </id>
    <property name="model_name" />
  </class>

</hibernate-mapping>

当我尝试将值发送到数据库时出现异常(在视图中它运行良好):+ $exception {“索引超出范围。必须是非负数并且小于集合的大小。\r \n参数名称:索引"} System.Exception {System.ArgumentOutOfRangeException}

我找不到这里可能有什么问题?

【问题讨论】:

    标签: c# asp.net nhibernate


    【解决方案1】:

    这里的问题在于双列映射:

    <property name="model" />    
    <many-to-one name="Modelis" column="model"  
         cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 
    

    两个属性(valueType 和 Reference)都针对同一列。这是可能的,但不适用于写操作。我们必须使用insert="false"update="false"

    将其中之一设为只读
    <property name="model" insert="false" update="false" />    
    <many-to-one name="Modelis" column="model"  
         cascade="none" class="web_nt.Models.Class.CarModel" lazy="false" /> 
    

    所以,现在我们确实可以访问映射到同一列的两个属性,但是 INSERT、UPDATE 将只针对该列一次。因为这是这里的原始问题: ...ndex 超出范围。必须是非负数且小于集合的大小...

    同时检查类似问题:https://stackoverflow.com/a/24248912/1679310

    【讨论】:

    • 酷!但是如果我使用模型将数据获取到 Tranport 列,我应该使用 insert/update false to many-to-one 选项,对吧?
    • + $exception {"无法插入:[web_nt.Models.Transport#173][SQL: INSERT INTO transport (lic_plate, model, odometer, moto_val, body_nr, color, made_date, autoage, buy_date , sell_date, travel_page, owner, region, rajonas, Id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"} 系统。异常 {NHibernate.Exceptions.GenericADOException} 我现在得到了这个......(自从我添加它们以来还有更多值)
    • 请扩展完整错误的答案。你知道吗?如果您使用切换只读模式会出现问题,那么我建议。因为在 INSERT 期间,如果可写是引用,它的 id 可以稍后创建(IDENTITY)并且 NHibernate 会关心。但是,如果我们设置瞬态实体的 ID(通常为 0),则 INSERT 最终会违反约束...
    • 我尝试了两种方式 - 你的方式和切换。我写了我得到的整个异常
    • 等等,我看不到整个异常!你能把它粘贴到你的问题中吗?!?这很重要,因为它位于 之后 ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]" } System.Exception {NHibernate.Exceptions.GenericADOException}...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多