【发布时间】:2012-10-11 01:51:08
【问题描述】:
以下代码块继续遇到“使用多映射 API 时,如果您有除 Id 以外的键,请确保设置 splitOn 参数”错误:
var accounts = DbConnection.Query<Account, Branch, Application, Account>(
"select Accounts.*, SplitAccount = '', Branches.*, SplitBranch = '', Applications.*" +
" from Accounts" +
" join Branches" +
" on Accounts.BranchId = Branches.BranchId" +
" join Applications" +
" on Accounts.ApplicationId = Applications.ApplicationId" +
" where Accounts.AccountId <> 0",
(account, branch, application) =>
{
account.Branch = branch;
account.Application = application;
return account;
}, splitOn : "SplitAccount, SplitBranch"
).AsQueryable();
我将 SplitAccount 和 SplitBranch 用于 splitOn 作为解决方法。
我错过了什么?
谢谢
编辑:
我已经清理了一些测试,下面是一个轻量级的类和一个新的查询:
public class AccountLight
{
public int AccountId { get; set; }
public string AccountNumber { get; set; }
public BranchLight Branch { get; set; }
public ApplicationLight Application { get; set; }
}
public class BranchLight
{
public int BranchId { get; set; }
public string BranchNumber { get; set; }
}
public class ApplicationLight
{
public int ApplicationId { get; set; }
public string ApplicationCode { get; set; }
}
var accounts2 = DbConnection.Query<AccountLight, BranchLight, ApplicationLight, AccountLight>(
"select Accounts.AccountId, Accounts.AccountNumber," +
" Branches.BranchId, Branches.BranchNumber," +
" Applications.ApplicationId, Applications.ApplicationCode" +
" from Accounts" +
" inner join Branches" +
" on Accounts.BranchId = Branches.BranchId" +
" inner join Applications" +
" on Accounts.ApplicationId = Applications.ApplicationId" +
" where Accounts.AccountId <> 0",
(account, brach, application) =>
{
account.Branch = brach;
account.Application = application;
return account;
},
commandType: CommandType.Text,
splitOn: "AccountId, BranchId"
).AsQueryable();
【问题讨论】:
-
你的解决方法很好......你的桌子上有什么主键?
-
山姆,感谢您的回复。以下是键:Account:AccountId 作为 PK,BranchId 和 ApplicationId 作为 FK 分支:BranchId 作为 PK 应用程序:ApplicationId 作为 PK
-
另一个注意事项:如果我删除应用程序,它会起作用。
标签: dapper