Slapper.Automapper 和 Dapper.net 使用 Guid ID
Slapper.Automapper with Dapper.net using Guid Ids
我需要使用 Dapper.net.
将一对多扁平化 SQL 查询映射到嵌套对象中
Slapper.Automapper 似乎是一个很好的方法;详见此问题的答案:
How do I write one to many query in Dapper.Net?
我使用 Guids 得到的错误数据集:
public string MrFlibble3()
{
using (var connection = new SqlConnection(Constr))
{
Slapper.AutoMapper.Cache.ClearInstanceCache();
const string sql = @"SELECT tc.[IDG] as ContactIdg
,tc.[ContactName] as ContactName
,tp.[Idg] AS TestPhones_PhoneIdg
,tp.[ContactIdg] AS TestPhones_ContactIdg
,tp.[Number] AS TestPhones_Number
FROM TestContact tc
INNER JOIN TestPhone tp ON tc.Idg = tp.ContactIdg";
// Step 1: Use Dapper to return the flat result as a Dynamic.
dynamic test = connection.Query<dynamic>(sql);
// Step 2: Use Slapper.Automapper for mapping to the POCO Entities.
// - IMPORTANT: Let Slapper.Automapper know how to do the mapping;
// let it know the primary key for each POCO.
// - Must also use underscore notation ("_") to name parameters;
// see Slapper.Automapper docs.
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(TestContact), new List<string> { "ContactIDg" });
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(TestPhone), new List<string> { "PhoneIdg" });
var testContact = (Slapper.AutoMapper.MapDynamic<TestContact>(test) as IEnumerable<TestContact>).ToList();
string flibblethis = "";
foreach (var c in testContact)
{
foreach (var p in c.TestPhones)
{
// Console.Write("ContactName: {0}: Phone: {1}\n", c.ContactName, p.Number);
flibblethis += "Contact Name: " + c.ContactName + ". Number: " + p.Number + "<br />";
}
}
return flibblethis;
}
}
它在示例中运行良好 - 除了如果 ID 是 Guid 而不是整数,Slapper.Automapper 似乎不起作用。
是否有任何方法可以将 Guid ID 与 Slapper.Automapper 一起使用 - 或者是否有其他方法可以使用 Dapper.net 来映射它?
(Slapper.Automapper 没有被广泛使用,我在网上看不到关于这个问题的任何信息)。
您的 属性 姓名中有不同的大写和小写字母,例如ContactIdg
和 ContactIDg
。 Slapper 进行区分大小写的映射,请参阅 this issue。
我需要使用 Dapper.net.
将一对多扁平化 SQL 查询映射到嵌套对象中Slapper.Automapper 似乎是一个很好的方法;详见此问题的答案:
How do I write one to many query in Dapper.Net?
我使用 Guids 得到的错误数据集:
public string MrFlibble3()
{
using (var connection = new SqlConnection(Constr))
{
Slapper.AutoMapper.Cache.ClearInstanceCache();
const string sql = @"SELECT tc.[IDG] as ContactIdg
,tc.[ContactName] as ContactName
,tp.[Idg] AS TestPhones_PhoneIdg
,tp.[ContactIdg] AS TestPhones_ContactIdg
,tp.[Number] AS TestPhones_Number
FROM TestContact tc
INNER JOIN TestPhone tp ON tc.Idg = tp.ContactIdg";
// Step 1: Use Dapper to return the flat result as a Dynamic.
dynamic test = connection.Query<dynamic>(sql);
// Step 2: Use Slapper.Automapper for mapping to the POCO Entities.
// - IMPORTANT: Let Slapper.Automapper know how to do the mapping;
// let it know the primary key for each POCO.
// - Must also use underscore notation ("_") to name parameters;
// see Slapper.Automapper docs.
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(TestContact), new List<string> { "ContactIDg" });
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(TestPhone), new List<string> { "PhoneIdg" });
var testContact = (Slapper.AutoMapper.MapDynamic<TestContact>(test) as IEnumerable<TestContact>).ToList();
string flibblethis = "";
foreach (var c in testContact)
{
foreach (var p in c.TestPhones)
{
// Console.Write("ContactName: {0}: Phone: {1}\n", c.ContactName, p.Number);
flibblethis += "Contact Name: " + c.ContactName + ". Number: " + p.Number + "<br />";
}
}
return flibblethis;
}
}
它在示例中运行良好 - 除了如果 ID 是 Guid 而不是整数,Slapper.Automapper 似乎不起作用。
是否有任何方法可以将 Guid ID 与 Slapper.Automapper 一起使用 - 或者是否有其他方法可以使用 Dapper.net 来映射它?
(Slapper.Automapper 没有被广泛使用,我在网上看不到关于这个问题的任何信息)。
您的 属性 姓名中有不同的大写和小写字母,例如ContactIdg
和 ContactIDg
。 Slapper 进行区分大小写的映射,请参阅 this issue。