我的遗留数据库中有两个表
购买
MasterAccounts
我有一个将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
答案 0 :(得分:1)
假设 Purchases 中的MasterAccount
字段与 MasterAccounts 中的MasterAccountNumber
匹配...在表格格式的sql中创建视图正试图映射:
CREATE VIEW [dbo].[v_PurchaseMasterAccountName]
AS
SELECT dbo.Purchases.Id, dbo.Purchases.Name, dbo.Purchases.MasterAccount, dbo.Purchases.BuyerAccount, dbo.MasterAccounts.Name AS MasterAccountName
FROM dbo.MasterAccounts INNER JOIN
dbo.Purchases ON dbo.MasterAccounts.MasterAccountNumber = dbo.Purchases.MasterAccount
创建一个ClassMap来映射你的视图:
public class Purchase
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string MasterAccountName { get; set; }
public virtual string MasterAccount { get; set; }
public virtual string BuyerAccount { get; set; }
}
public class PurchaseClassMap : ClassMap<Purchase>
{
public PurchaseClassMap()
{
Table("v_PurchaseMasterAccountName");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.MasterAccount);
Map(x => x.BuyerAccount);
Map(x => x.MasterAccountName);
}
}
确保在FluentMappings中选择了ClassMap。
<强>更新强>
这可能会改为:
public class PurchaseMap : ClassMap<Purchase>
{
public PurchaseMap()
{
Table("Purchases");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.MasterAccount);
Map(x => x.BuyerAccount);
Map(x => x.MasterAccountName).ReadOnly()
.Formula("(SELECT TOP 1 MasterAccounts.Name FROM MasterAccounts WHERE MasterAccounts.MasterAccountNumber = [MasterAccount] AND MasterAccounts.MasterAccountNumber = [BuyerAccount])");
}
}
感谢Darren Kopp指向公式选项的指针。
答案 1 :(得分:0)
这是一个不寻常的问题,可能还有其他选择,但这是我的几个想法。