不使用主键的 Castle ActiveRecord 关系

发布于 2024-11-05 09:41:25 字数 985 浏览 0 评论 0原文

我正在将 Castle ActiveRecord 连接到旧数据库,但在连接一对多关系时遇到问题。问题是外键没有引用另一个表的主键,它使用另一个列。

这些是表(为清楚起见而缩短):

CREATE TABLE [Rule](
    [ID] [uniqueidentifier] NOT NULL,
    [HeadingID] [int] NULL
)

CREATE TABLE [Heading](
    [ID] [uniqueidentifier] NOT NULL,
    [ID2] [int] NOT NULL
)

规则表中的 HeadingID 字段是引用标题中的 ID2 字段的外键。

因此,在 Rule 类的定义中,我有:

[BelongsTo(Column = "HeadingID", PropertyRef = "OrderID")]
public virtual Heading Heading { get; set; }

这似乎工作正常,我可以毫无问题地访问规则的标题(当然,如果我将 HasMany 设置为惰性)。

在 Heading 类定义中,我有:

[HasMany(Lazy = true)]
public IList<Rule> Rules { get; set; }

当我访问集合时,我收到 SQL 异常“操作数类型冲突:uniqueidentifier 与 int 不兼容。” 看起来 AR 正在尝试进行这样的选择(为了清晰起见,进行了简化):

SELECT ... FROM Rule rules0_ ... WHERE rules0_.HeadingID = ?

哪里?是 Heading.ID 中的 GUID(应该是 Heading.ID2 中的 int)。

我找不到 HasMany 的属性,该属性允许我设置外键引用的列。有什么办法可以做到这一点吗?

I am connecting Castle ActiveRecord to a legacy database and I am having trouble wiring up a One-To-Many relationship. The problem is that the foreign key does not reference the primary key of the other table, it uses another column.

These are the tables (shortened for clarity):

CREATE TABLE [Rule](
    [ID] [uniqueidentifier] NOT NULL,
    [HeadingID] [int] NULL
)

CREATE TABLE [Heading](
    [ID] [uniqueidentifier] NOT NULL,
    [ID2] [int] NOT NULL
)

The HeadingID field in the Rule table is a foreign key which references the ID2 field in Heading.

So, in the definition of the Rule class I have:

[BelongsTo(Column = "HeadingID", PropertyRef = "OrderID")]
public virtual Heading Heading { get; set; }

This seems to work fine, I can access the Heading of a Rule with no problem (if I set the HasMany lazy of course).

In the Heading class definition I have:

[HasMany(Lazy = true)]
public IList<Rule> Rules { get; set; }

When I access the collection I get an SQL exception "Operand type clash: uniqueidentifier is incompatible with int."
It looks like AR is attempting to do a select like this (simplified for clarity):

SELECT ... FROM Rule rules0_ ... WHERE rules0_.HeadingID = ?

Where ? is the GUID from Heading.ID (it should be the int from Heading.ID2).

I can't find a property for HasMany that allows me to set the column to which the foreign key refers. Is there any way to do this?

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

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

发布评论

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

评论(1

情绪少女 2024-11-12 09:41:25

看来这是做不到的。我能做的最好的事情就是自定义查找:

public virtual IEnumerable<Rule> Rules {
    get {
        return Rule.Queryable.Where(x => x.Heading == this);
    }
}

这对我来说已经足够好了。

It appears that this cannot be done. The best I could do was a custom find:

public virtual IEnumerable<Rule> Rules {
    get {
        return Rule.Queryable.Where(x => x.Heading == this);
    }
}

This works well enough for me.

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