实体框架代码优先-“多态”关联列
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该创建两个可为空的 FK,而不是像这样的弱引用。
You probably should create two nullable FKs instead of weak reference like that.
呃...尴尬开始了...我可以以相同的方式映射它,而不管任何实际的数据库外键如何。
我在尝试这样做时遇到了问题,但基本上与此无关。我有计算属性,我没有要求上下文忽略它生成错误的查询。
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.