实体框架代码优先-“多态”关联列

发布于 2024-12-09 19:23:26 字数 461 浏览 0 评论 0原文

我有 3 个表:

1.发票

InvoiceID int 主键

2.订单

OrderID int 主键

3.交易

TransactionID int 主键

Source int

Category string

在表“交易”上,源(不幸的是)表现为“多态”(?? )外键(必须有一个实际术语 - 抱歉我的无知),根据类别列,它将包含以下 ID发票或订单。

但是没有实际的外键。

首先使用 EF 4.1 代码,有人知道我将如何创建正确的关联吗?

感谢帮助! 谢谢

解决方案

呃...尴尬开始了...我可以以相同的方式映射它,无论任何实际的数据库外键如何。

我在尝试这样做时遇到了问题,但基本上与此无关。我有计算属性,我没有要求上下文忽略它生成错误的查询。

I have 3 tables:

1. Invoice

InvoiceID int PRIMARY KEY

2. Order

OrderID int PRIMARY KEY

3. Transaction

TransactionID int PRIMARY KEY

Source int

Category string

On table "Transaction", Source (unfortunately) is behaving as a "polymorphic"(??) foreign key (there must be an actual term for that - sorry for my ignorance) that depending on the Category column it'll contain the ID of Invoice or Order.

However there's no actual foreign key.

Using EF 4.1 code first, anyone has any idea how I would create the proper associations?

Help is appreciated!
Thanks

Solution

Uh... Embarrassment is kicking in... I can just map it same way regardless of any actual DB foreign key.

I was having problems while trying to do that but basically wasn't related to this. I had computation properties that I didn't ask the context to ignore which was generating wrong queries.

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

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

发布评论

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

评论(2

草莓味的萝莉 2024-12-16 19:23:26

您可能应该创建两个可为空的 FK,而不是像这样的弱引用。

You probably should create two nullable FKs instead of weak reference like that.

以往的大感动 2024-12-16 19:23:26

呃...尴尬开始了...我可以以相同的方式映射它,而不管任何实际的数据库外键如何。

我在尝试这样做时遇到了问题,但基本上与此无关。我有计算属性,我没有要求上下文忽略它生成错误的查询。

Uh... Embarrassment is kicking in... I can just map it same way regardless of any actual DB foreign key.

I was having problems while trying to do that but basically wasn't related to this. I had computation properties that I didn't ask the context to ignore which was generating wrong queries.

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