Slapper.AutoMapper returns 对象列表中只有一个结果
Slapper.AutoMapper returns only single result from list of objects
我有两个 POCO-s。 A 和 B.
public class A{
[Slapper.AutoMapper.Id]
public int Id { get; set; }
public B BType { get; set; }
// Rest of the fields
}
public class B{
[Slapper.AutoMapper.Id]
public int Id { get; set; }
public string Name {get;set;}
}
T-SQL 存储过程结果:
ID B_Id B_Name
1 1 B1
2 2 B2
我用 Dapper 获取列表的代码是:
List<A> aList = new List<A>();
using (IDbConnection connection = new SqlConnection(GlobalConfig.ConnectionString()))
{
var p = new DynamicParameters();
p.Add("@Id", someId);
// Here debug shows me 2 correct objects inside of var list
var list = connection.Query<dynamic>("[spApp_getBlaBlaByID]", p, commandType: CommandType.StoredProcedure);
// After casting I got only 1
aList =(Slapper.AutoMapper.MapDynamic<A>(list) as IEnumerable<A>).ToList();
}
return aList; // Debugging shows only 1 one of the objects
所以,我的代码有什么问题?请帮忙找出错误。
P.S。我从 Java 来到 C#。也许在 C# 世界中 Slapper.Automapper 不再流行。用 DAPPER 映射 POCO-s 的灵活和现代的解决方案是什么?
首先,我不明白这里需要 Slapper。 Dapper 能够胜任这个角色。所以像下面这样的代码 something 应该可以在没有 Slapper 的情况下工作。
List<A> list = connection.Query<A>("[spApp_getBlaBlaByID]"......
其次是@orhtej2在评论中提到的。存储过程的名称以 "ByID" 结尾。此 约定 表明它将 return 单条记录。您确定 SP return 有多个记录吗?如果是,上面的代码示例应该可以。如果它 return 是单条记录,请将上面的内容更改为如下所示:
A a = connection.Query<A>("[spApp_getBlaBlaByID]"......
我有两个 POCO-s。 A 和 B.
public class A{
[Slapper.AutoMapper.Id]
public int Id { get; set; }
public B BType { get; set; }
// Rest of the fields
}
public class B{
[Slapper.AutoMapper.Id]
public int Id { get; set; }
public string Name {get;set;}
}
T-SQL 存储过程结果:
ID B_Id B_Name 1 1 B1 2 2 B2
我用 Dapper 获取列表的代码是:
List<A> aList = new List<A>();
using (IDbConnection connection = new SqlConnection(GlobalConfig.ConnectionString()))
{
var p = new DynamicParameters();
p.Add("@Id", someId);
// Here debug shows me 2 correct objects inside of var list
var list = connection.Query<dynamic>("[spApp_getBlaBlaByID]", p, commandType: CommandType.StoredProcedure);
// After casting I got only 1
aList =(Slapper.AutoMapper.MapDynamic<A>(list) as IEnumerable<A>).ToList();
}
return aList; // Debugging shows only 1 one of the objects
所以,我的代码有什么问题?请帮忙找出错误。 P.S。我从 Java 来到 C#。也许在 C# 世界中 Slapper.Automapper 不再流行。用 DAPPER 映射 POCO-s 的灵活和现代的解决方案是什么?
首先,我不明白这里需要 Slapper。 Dapper 能够胜任这个角色。所以像下面这样的代码 something 应该可以在没有 Slapper 的情况下工作。
List<A> list = connection.Query<A>("[spApp_getBlaBlaByID]"......
其次是@orhtej2在评论中提到的。存储过程的名称以 "ByID" 结尾。此 约定 表明它将 return 单条记录。您确定 SP return 有多个记录吗?如果是,上面的代码示例应该可以。如果它 return 是单条记录,请将上面的内容更改为如下所示:
A a = connection.Query<A>("[spApp_getBlaBlaByID]"......