在Castle Active Record中创建2个相同的外键

时间:2012-01-17 04:52:00

标签: castle-activerecord

我这里有2个实体

让我们说

飞行 flight_id - PK 起源 - FK1 目的地 - FK2

国家 country_id - PK 国家 代码

示例代码 班级飞行 {       public int ID {get;组; }

  [BelongsTo(Column = "Origin", ForeignKey = "country_id")]
  public Countries Origin {get; set;}

  [BelongsTo(Column = "destination", ForeignKey = "country_id")]
  public Countries Destination {get; set; }

}

我在Activerecord上创建架构时遇到错误。什么是替代品呢?谢谢!

1 个答案:

答案 0 :(得分:2)

您在BelongsTo属性中将ForeignKey参数命名为两个键都相同。此参数不是您要使用的列的名称,而是ActiveRecord在创建架构时用于命名的约束的名称。

我做了一些假设并使用一个有效的示例扩展了您的代码示例:

[ActiveRecord]
public class Flight
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo(Column = "Origin", ForeignKey = "country_id_origin")]
    public Countries Origin { get; set; }

    [BelongsTo(Column = "Destination", ForeignKey = "country_id_destination")]
    public Countries Destination { get; set; }
}

[ActiveRecord]
public class Countries 
{
    [PrimaryKey]
    public int country_id { get; set; }

    [Property]
    public string CountryName { get; set; }
}