我有一个对象:
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; }
}
但是,当我使用Dapper调用以下查询时:
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");
重要说明:默认情况下,Dapper使用Id,这就是我将Code重命名为Id的原因,但即使使用Code或重命名,我仍然会收到多映射错误。
答案 0 :(得分: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"));
}
答案 1 :(得分:0)
所以,我发现它失败的主要原因。 Dapper不喜欢在splitOn
和int
之间交替的string
参数。通过强制所有这些统一起来,它起作用了。我注意到的另一个问题是,如果你有一个名为Code
的列,它反映了一个主键但SQL没有设置为关系标识符,那么它也会出错。
经过纠正后没问题。