NHibernate一到多个多列

本文关键字:NHibernate | 更新日期: 2023-09-27 18:25:44

我在映射这个。。。这可能吗?

这里有一个例子:

public class Location{
    public int ID {get;set;}
    public float Latitude {get;set;}
    public float Longitude {get;set;}
    public IEnumerable<Weather> Weather {get;set;}
}
public class Weather{
    public int ID {get;set;}
    public float Latitude {get;set;}
    public float Longitude {get;set;}
    public DateTime TimeMeasured {get;set;}
    public float Temperature {get;set;}
    ...
}

所以我想要LocationWeather实体之间的关系。它们可以通过LatitudeLongitude属性连接,但我不知道如何映射。

这就是我尝试过的:

<class table="locations" name="Model.Location, Model">
...
<set name="Weather" lazy="extra">
  <key>
    <column name="Latitude" />
    <column name="Longitude" />
  </key>
  <one-to-many class="Model.Weather, Model" />
</set>
...
</class>

它抛出一个:

Foreign key (FK911522E81A761796:weather [Latitude, Longitude])) must have same number of columns as the referenced primary key (locations [ID]):

NHibernate一到多个多列

也许这篇关于具有关联的复合键的NHibernate映射的文章可以帮助您?看起来您需要定义复合密钥:

  <class name="Product" table="Products" lazy="true" >
    <composite-id name="ProductIdentifier" class="ProductIdentifier">
      <key-property name="StoreID" column="StoreID" />
      <key-property name="ProductID" column="ProductID" />
    </composite-id>
    <set name="Orders" inverse="true">
      <key>
        <column name="StoreID"/>
        <column name="ProductID"/>
      </key>
      <one-to-many class="Order"/>
    </set>
  </class>