我有一个目标:小巧玲珑多映射
public class Species : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
public SpeciesCategory Category { get; set; }
public WetlandIndicator WetlandIndicator { get; set; }
}
public class SpeciesCategory : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
}
public class WetlandIndicator : IEntity<string>
{
public string Id { get; set; }
public string Designation { get; set; }
public bool Status { get; set; }
}
然而,当我用小巧玲珑来调用下面的查询:
SELECT
[Species].*,
[SpeciesType].*,
[WetlandIndicator].Code AS Id,
[WetlandIndicator].Designation
FROM
((([Watershed].[Vegetation].[Species] INNER JOIN [Vegetation].[SpeciesCategory]
ON [Watershed].[Vegetation].[Species].[SpeciesCategoryId] = [Vegetation].[SpeciesCategory].[Id]) INNER JOIN [Watershed].[Vegetation].[SpeciesType]
ON [Watershed].[Vegetation].[Species].[SpeciesTypeId] = [Vegetation].[SpeciesType].[Id]) INNER JOIN [Watershed].[Vegetation].[WetlandIndicator]
ON [Vegetation].[Species].[WetlandIndicatorCode] = [Vegetation].[WetlandIndicator].[Code])
我收到使用多映射时确保,请确保您使用splitOn
属性。我是谁,但我仍然收到错误。所以我假设我有一些类型的使用错误或合成文本错误。 ,不断的错误是因为代码如下:
public async Task<IEnumerable<SpeciesDomain>> GetAllSpecies(string query) =>
await dbConnection.QueryAsync<Species, SpeciesCategory, WetlandIndicator, SpeciesDomain>(query, (species, speciesCategory, wetlandIndicator) =>
{
species.SpeciesCategory = speciesCategory;
species.WetlandIndicator = wetlandIndicator;
return species;
}, splitOn: "Id, Code");
重要提示:默认情况下小巧玲珑利用标识,这就是为什么我改名守则ID,但即使有代码或重命名我仍然收到了多映射错误。
只是为了澄清他人:如果您使用不同数据类型的字段进行拆分,Dapper不会失败。我会更新我的演示答案。 –
我需要澄清,如果你有字段:'代码'和一个名为'代码'问题的Id列。 – Greg