我有一個目標:小巧玲瓏多映射
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