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

发布于 2024-12-27 09:25:56 字数 447 浏览 0 评论 0原文

我这里有 2 个实体,

比如说

Flight Flight_id - PK 起源-FK1 目的地 - FK2

国家 Country_id - PK 国家 代码

示例代码 舱位 飞行 { 公共 int ID {获取;放; 。

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

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

在 Activerecord 上创建架构

时出现错误 有什么替代方案吗?谢谢!

I have here 2 entities

let say

Flight
flight_id - PK
origin - FK1
destination - FK2

Countries
country_id - PK
country
code

sample code
Class Flight
{
public int ID {get; set; }

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

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

}

I'm getting an error when creating a schema on Activerecord. What will be an alternative to this? thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2025-01-03 09:25:56

您将两个键的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; }
}

You're naming the ForeignKey parameter in the BelongsTo attribute the same for both keys. This parameter is not the name of the column you want to use, but actually the name of the constraint that ActiveRecord uses to name it when creating the schema.

I've made some assumptions and expanded your code sample with an example that works:

[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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文