如何在 Fluent Nhibernate 中对两个名称不匹配的非键属性创建联接?
我的旧数据库中有两个表
Purchasing
- Id int(PK)
- name varchar(50)
- MasterAccount char(10)
- BuyerAccount char(10)
MasterAccounts
- Id int(PK)
- Name varchar(50)
- MasterAccountNumber char(10)
- AccountNumber char(10)
我有一个将 1:1 映射到购买表的对象。我想将 MasterAccounts 表中的“名称”列作为属性添加到购买对象中。
当我想要连接的两列时,如何告诉 Fluent Nhibernate 执行连接:
- 未在数据库中定义为外键
- 每个表中的名称不相同
查看我想要的 SQL 可能会有所帮助产生。
Select Purchases.*, MA.Name from Purchases left join MasterAccounts MA on MA.MasterAccountNumber = Purchases.MasterAccount and MA.AccountNumber = Purchases.BuyerAccount
I have two tables in my legacy database
Purchases
- Id int(PK)
- name varchar(50)
- MasterAccount char(10)
- BuyerAccount char(10)
MasterAccounts
- Id int(PK)
- Name varchar(50)
- MasterAccountNumber char(10)
- AccountNumber char(10)
I have an object that maps 1:1 to the purchases table. I want to add the "Name" column from the MasterAccounts table to the purchases object as a property.
How do I tell Fluent Nhibernate to perform a join when the two columns I want to join on:
- are not defined as foreign keys in the database
- Do not have the same name in each table
It might be helpful to see the SQL that I want to generate.
Select Purchases.*, MA.Name from Purchases left join MasterAccounts MA on MA.MasterAccountNumber = Purchases.MasterAccount and MA.AccountNumber = Purchases.BuyerAccount
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 Purchases 中的
MasterAccount
字段与 MasterAccounts 中的MasterAccountNumber
匹配...在 sql 中创建一个视图,其中包含您尝试映射的表格式:创建一个 ClassMap 来映射您的视图:
确保您的 ClassMap 在您的 FluentMappings 中被选取。
更新:
这可能会起作用:
感谢 Darren Kopp 指向“公式”选项的指针。
Assuming that the
MasterAccount
field in Purchases matches theMasterAccountNumber
in MasterAccounts...create a view in sql with the table format you are trying to map:Create a ClassMap to map your view:
Ensure your ClassMap is picked up in your FluentMappings.
UPDATE:
This may work instead:
Thanks to Darren Kopp for the pointer to the Formula option.
这是一个不寻常的问题,很可能还有其他选择,但这是我的一些想法。
This is kind of an unusual question and there may very well be other options, but here's my couple of ideas.