【问题标题】:NHibernate one-to-many multiple columnsNHibernate 一对多多列
【发布时间】:2014-10-07 10:50:12
【问题描述】:

我在映射这个时遇到了问题……这有可能吗?

这是一个例子:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好的。但是我可以定义一个复合键而不是主键吗?因为天气表只有纬度、经度和时间测量是唯一的。仅有经纬度是不够的
    • 嗯,我不认为这是可能的。但是在 Weather 表中使用 FK_Location_ID 会不会更有意义,从而避免纬度和经度的重复?也使您的映射更加简单。
    • 不,因为有许多位置具有相同的坐标。它已经是一张巨大的桌子,至少可以乘以 10 倍
    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多