[英]How to map many to many relation to a list with dapper
I have a class AnalysisRule 我有一个类AnalysisRule
public class AnalysisRule
{
public long Id { get; set; }
public Analysis Analysis { get; set; }
public AnalysisCategory AnalysisCategory { get; set; }
public Gender Gender { get; set; }
public bool FatherHerdBookRequired { get; set; }
public bool MotherHerdBookRequired { get; set; }
public List<Breed> AllowedBreeds { get; set; }
}
That has a list of Breeds 那有一个品种清单
public class Breed
{
public long BreedId { get; set; }
public long AnimalTypeId { get; set; }
public long BreedCode { get; set; }
public string BreedName { get; set; }
public string BreedAcronym { get; set; }
}
This is a many to many relationship that I bind together with a DB table AnalysisRulesBreeds 这是我与数据库表AnalysisRulesBreeds绑定在一起的多对多关系
Breeds 品种
With Dapper I have tried 我尝试过使用Dapper
var sql = @"select *
from ""AnalysisRules""
join ""AnalysisCategory"" on ""AnalysisRules"".""AnalysisCategoryId"" = ""AnalysisCategory"".""Id""
join ""Analysis"" on ""AnalysisRules"".""AnalysisId"" = ""Analysis"".""Id""
left join ""AnalysisRulesBreeds"" on ""AnalysisRulesBreeds"".""AnalysisRuleId"" = ""AnalysisRules"".""Id""
left join ""Breed"" on ""AnalysisRulesBreeds"".""BreedId"" = ""Breed"".""BreedId""
where ""AnalysisId"" = :AnalysisId";
rules = sqlConnection.QueryAsync<AnalysisRule, AnalysisCategory, Analysis, Breed, AnalysisRule>(
sql,
(ar, c, a, b) =>
{
ar.AnalysisCategory = c;
ar.Analysis = a;
ar.Breeds.Add(b);
return ar;
},
new
{
AnalysisId = analysisId
},
splitOn:"BreedId");
Which gives me 这给了我
´When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn ´使用多重映射API时,如果您具有Id以外的其他键,请确保设置splitOn参数。参数名称:splitOn
If I run the same query in SQL Developer I get 2 rows out with same Id but with different data in Breed, so the query should be good enough. 如果我在SQL Developer中运行相同的查询,则会得到2行,且具有相同的ID,但Breed中的数据不同,因此该查询应该足够好。
So how do I get these 2 rows into one AnalysisRule entity where Breeds consist of 2 Breed entities? 那么,如何将这2行放入一个AnalysisRule实体中,其中Breeds由2个Breed实体组成?
EDIT I now have 编辑我现在有
sqlConnection.Open();
var sql = @"select ar.*,
ac.*,
b.*
from ""AnalysisRules"" ar
join ""AnalysisCategory"" ac on ar.""AnalysisCategoryId"" = ac.""Id""
join ""Analysis"" a on ar.""AnalysisId"" = a.""Id""
left join ""AnalysisRulesBreeds"" on ""AnalysisRulesBreeds"".""AnalysisRuleId"" = ar.""Id""
left join ""Breed"" b on ""AnalysisRulesBreeds"".""BreedId"" = b.""Id""
where ""AnalysisId"" = :AnalysisId";
var rules = sqlConnection.QueryAsync<AnalysisRule, AnalysisCategory, Analysis, Breed, AnalysisRule>(
sql,
(ar, c, a, b) =>
{
ar.AnalysisCategory = c;
ar.Analysis = a;
ar.Breeds.Add(b);
return ar;
},
new
{
AnalysisId = analysisId
});
return await rules;
Removed the splitOn, changed AnalysisRulesBreedsId to Id but I still get 删除了splitOn,将AnalysisRulesBreedsId更改为ID,但我仍然得到
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn 使用多重映射API时,如果您具有Id以外的其他键,请确保设置splitOn参数参数名称:splitOn
If I do the same query in SQLDev I get 如果我在SQLDev中执行相同的查询,则会得到
By selecting * you get the columns of every joined table. 通过选择*,您将获得每个联接表的列。 Also you set splitOn
to BreedId
. 另外,您将splitOn
设置为BreedId
。 Now Dapper expects that to separate the row columns of one joined table from the next, it should look for a column named BreedId
. 现在,Dapper希望将一个联接表的行列与下一个联接表的行列分开,它应该查找名为BreedId
的列。
This does not work because all tables except AnalysisRulesBreeds
use Id
as id column name. 这不起作用,因为除AnalysisRulesBreeds
之外的所有表都使用Id
作为id列名。
Try removing the splitOn
parameter, then it will default to Id
. 尝试删除splitOn
参数,然后它将默认为Id
。 Then adjust your select-clause to only select from the tables you actually need in the result, eg. 然后调整选择子句,使其仅从结果中实际需要的表中进行选择,例如。
select AnalysisRule.*, AnalysisCategory.*, Analysis.*, Breed.*
(assuming that your Analysis table and AnalysisCategory table follow the convention of having an Id column named ´Id´). (假设您的Analysis表和AnalysisCategory表遵循具有名为“ Id”的ID列的约定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.