gpt4 book ai didi

c# - NHibernate 映射索引超出范围

转载 作者:行者123 更新时间:2023-12-02 20:04:11 26 4
gpt4 key购买 nike

我有两个类(class):

 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参数名称:index"} System.Exception {System.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

关于c# - NHibernate 映射索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com