问题描述
我有一个可能如下所示的结果集:
I have a result set that might look like this:
ID (no column name) anotherID
---- ---------------- ----------
1 super 3
1 super 4
3 duper 6
4 really 7
4 really 8
我有2个问题:
第一:如何将dapper与没有名字的列一起使用?
First: How do I use dapper with a column with no name?
第二个:我想建立一个父子关系,这样我得到3个对象,每个对象都带有anotherID的列表,例如:
Second: I want to have a parent child relationship such that I get 3 objects each with a list of anotherID's for example:
public class MyObject
{
public int ID
public string Name
public int[] Children
}
推荐答案
dapper不支持未命名的列。我从来没有真正看到它们的原因。
Well, un-named columns are not supported by dapper. I never really saw a reason for them.
我想我们可以为以下项目建立支持:
I guess we could build support for:
class Foo { [ColumnNumber(1)] public string Name {get;set;} }
问题在于它引入了一种非常脆弱的查询方法,我强烈不喜欢这种方法,将指令传递给 Query
同样笨拙。
The trouble is that it introduces a very fragile method of querying which I strongly dislike, passing a directive to Query
is just as clunky.
但是,如果您乐于改变获取结果的方式,则可以解决此问题。
However, if you are happy to change the way you grab the results you could work around this.
var grid = QueryMultiple(@"set nocount on
declare @t table(Id int, Name nvarchar(max), AnotherId int)
insert @t
exec proc
set nocount off
select Id, Name from @t
select Id, AnotherId from @t
");
然后在此处使用该技术进行多重映射:
Then use the technique here to multi map: Multi-Mapper to create object hierarchy
这篇关于Dapper-dot-net“无列名”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!