Dapper-dot-net “no column name”

与世无争的帅哥 提交于 2019-12-13 15:12:14

问题


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

I have 2 issues:

First: How do I use dapper with a column with no name?

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
}

回答1:


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;} }

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



来源:https://stackoverflow.com/questions/7326375/dapper-dot-net-no-column-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!