gpt4 book ai didi

c# - NHibernate <一对多>

转载 作者:太空狗 更新时间:2023-10-29 23:22:37 25 4
gpt4 key购买 nike

如何映射这两个类?

public class LDAVehicle 
{
public virtual int VehicleId { get; set; }
public virtual string ChassisSeries { get; set; }
public virtual string ChassisNumber { get; set; }

public virtual List<LDAReading> Readings { get; set; }
}

public class LDAReading
{
public virtual int ReadingId { get; set; }
public virtual DateTime IncomingDate { get; set; }
public virtual DateTime ReadoutDate { get; set; }
public virtual string Sender { get; set; }

public virtual LDAVehicle Vehicle { get; set; }
}

实际上我有这个 xml:

<class name="LDAVehicle" table="Vehicle" schema="dbo" lazy="false">
<id name="VehicleId">
<column name="VehicleId" />
<generator class="native"></generator>
</id>
<property name="ChassisSeries" column="ChassisSeries" not-null="false" />
<property name="ChassisNumber" column="ChassisNumber" not-null="false" />

<set name="Readings" table="Reading" fetch="join">
<key foreign-key="VehicleId" />
<one-to-many class="LDAReading" />
</set>
</class>

<class name="LDAReading" table="Reading" schema="dbo" lazy="false">
<id name="ReadingId">
<column name="ReadingId" />
<generator class="native"></generator>
</id>
<property name="IncomingDate" column="IncomingDate" not-null="true" />
<property name="ReadoutDate" column="ReadoutDate" not-null="true" />
<property name="Sender" column="Sender" not-null="false" />
<many-to-one name="Vehicle" class="LDAVehicle" fetch="select" column="VehicleId" />
</class>

但是我得到一个错误:

An association from the table Reading refers to an unmapped class

最佳答案

我会做一些改进来让它运行

1) C#

如果我们映射集合,它们必须表示为接口(interface),因此 NHibernate 将能够注入(inject)代理...自己的实现

public class LDAVehicle 
{
public virtual int VehicleId { get; set; }
public virtual string ChassisSeries { get; set; }
public virtual string ChassisNumber { get; set; }

//public virtual List<LDAReading> Readings { get; set; }
public virtual IList<LDAReading> Readings { get; set; }
}

public class LDAReading
{
public virtual int ReadingId { get; set; }
public virtual DateTime IncomingDate { get; set; }
public virtual DateTime ReadoutDate { get; set; }
public virtual string Sender { get; set; }

public virtual LDAVehicle Vehicle { get; set; }
}

2) 人体健康

对于映射集合,我会使用 bag (在 NHibernate 4 发布之前,我们需要使用 iesi 集合……所以 <bag> 现在已经足够好了)

此外,如果属性名称和列相同,则不必提及列。

最后,所有映射现在都是强惰性的。这就是要走的路

<class name="LDAVehicle" table="Vehicle" schema="dbo" lazy="true"> // should be lazy
<id name="VehicleId" column="VehicleId" generator="native" /> // simple id def

<property name="ChassisSeries" not-null="false" /> // name is enough
<property name="ChassisNumber" not-null="false" /> // if column is the same

// IList and <bag> go work together
// also profit from lazy loading,
// batch size fetching and inverse mapping
<bag name="Readings" lazy="true" inverse="true"
batch-size="25" cascade="all-delete-orphan">
<key column="VehicleId" />
<one-to-many class="LDAReading" />
</bag>
</class>

<class name="LDAReading" table="Reading" schema="dbo" lazy="true">
<id name="ReadingId" generator="native" />

<property name="IncomingDate" not-null="true" />
<property name="ReadoutDate" not-null="true" />
<property name="Sender" not-null="false" />

<many-to-one name="Vehicle" class="LDAVehicle" column="VehicleId" />
</class>

3) 确保两个映射都是嵌入式资源。如果未找到其中一个...未嵌入...抛出问题中的异常...此外,两个文件都应该/必须具有后缀 .hbm.xml

更多阅读:

关于c# - NHibernate <一对多>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008489/

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