NHibernate一对多多列

时间:2014-10-07 10:50:13

标签: c# .net nhibernate orm nhibernate-mapping

我无法映射这个......它甚至可能吗?

以下是一个例子:

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])

1 个答案:

答案 0 :(得分:0)

也许NHibernate Mappings for Composite Keys with Associations上的这篇文章对你有帮助吗?看起来您需要定义组合键:

  <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>