2017-08-07 159 views
1

我有一個目標:小巧玲瓏多映射

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,但即使有代碼或重命名我仍然收到了多映射錯誤。

回答

2

看起來你只需要從splitOn中刪除'代碼':「Id,Code」。您的查詢將其重命名爲「Id」。

Dapper還使用「Id」作爲默認值,因此不需要指定。

Dapper能夠通過假設 您的Id列被命名爲Id或id來拆分返回的行。如果您的主鍵不同於 或者您想要在除Id以外的其他位置拆分該行,請使用 可選splitOn參數。

下面是一個快速測試來驗證:

using (var conn = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo")) 
{ 
    var result = conn.Query<Species, SpeciesCategory, WetlandIndicator, Species>(
     "select Id = 11, Name = 'Foo', Id = 22, Name = 'Bar', Id = 33, Designation = 'House Cat' ", 
     (species, speciesCategory, wetlandIndicator) => 
    { 
     species.Category = speciesCategory; 
     species.WetlandIndicator = wetlandIndicator; 
     return species; 
    }).First(); 

    Assert.That(result.Id, Is.EqualTo(11)); 

    Assert.That(result.Category.Id, Is.EqualTo(22)); 
    Assert.That(result.Category.Name, Is.EqualTo("Bar")); 

    Assert.That(result.WetlandIndicator.Id, Is.EqualTo(33)); 
    Assert.That(result.WetlandIndicator.Designation, Is.EqualTo("House Cat")); 
} 

更新演示不同領域和類型

public class Species 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public SpeciesCategory Category { get; set; } 
    public WetlandIndicator WetlandIndicator { get; set; } 
} 

public class SpeciesCategory 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class WetlandIndicator 
{ 
    public string Code { get; set; } 
    public string Designation { get; set; } 
    public bool Status { get; set; } 
} 

using (var conn = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo")) 
{ 
    var result = conn.Query<Species, SpeciesCategory, WetlandIndicator, Species>(
     "select Id = 11, Name = 'Foo', Id = 22, Name = 'Bar', Code = 'X', Designation = 'House Cat' ", 
     (species, speciesCategory, wetlandIndicator) => 
    { 
     species.Category = speciesCategory; 
     species.WetlandIndicator = wetlandIndicator; 
     return species; 
    }, splitOn: "Id, Code").First(); 

    Assert.That(result.Id, Is.EqualTo(11)); 

    Assert.That(result.Category.Id, Is.EqualTo(22)); 
    Assert.That(result.Category.Name, Is.EqualTo("Bar")); 

    Assert.That(result.WetlandIndicator.Code, Is.EqualTo("X")); 
    Assert.That(result.WetlandIndicator.Designation, Is.EqualTo("House Cat")); 
} 
0

分裂所以,我發現它失敗的主要原因。 Dapper不喜歡splitOn參數在intstring之間交替。通過迫使他們統一起來,它就起作用了。我注意到的另一個項目是,如果你有一個名爲Code的列,例如鏡像一個主鍵但SQL沒有設置爲關係標識符,它也會出錯。

經過糾正後,沒有問題。

+0

只是爲了澄清他人:如果您使用不同數據類型的字段進行拆分,Dapper不會失敗。我會更新我的演示答案。 –

+0

我需要澄清,如果你有字段:'代碼'和一個名爲'代碼'問題的Id列。 – Greg