我有两个类:
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{"索引超出范围。必须非负且小于集合的大小。rnParameter name: index"}系统。异常{系统。ArgumentOutOfRangeException}
我找不到这里可能有什么问题?
这里的问题是在双列映射:
<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只针对该列一次。因为这是最初的问题:…指数超出范围。必须非负且小于集合的大小…
也检查类似的问题:https://stackoverflow.com/a/24248912/1679310