Dapper列号而不是列名?

时间:2012-07-28 18:28:42

标签: .net dapper

我对Dapper-dot-net "no column name"提出了一个非常类似的问题,但答案是没有让我到达我需要的地方。

我正在编写一个Web界面,并使用dapper从我客户的ERP系统中获取存储过程中的数据。 SP返回4列数据而没有列名。据说SP被锁定了,我无法改变它们。 我试图通过在Sam的建议中使用我的查询中的临时表来解决这个问题。

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
");

但是,我现在发现原始SP还包含一个用于记录的插入,因此SQL不允许我将sp插入到临时表中。

提到添加支持:

class Foo { [ColumnNumber(1)] public string Name {get;set;} }

我该怎么做?有人能指出我正确的方向修改Dapper源不需要列名,并允许我按列号映射?

1 个答案:

答案 0 :(得分:3)

非平凡约束的问题不断出现;最近出现在这里:http://code.google.com/p/dapper-dot-net/issues/detail?id=108

我的建议有,但我还没来得及编码。我提出了一个静态事件,你可以选择处理,并应用你想要的任何深奥的映射,即

SqlMapper.ColumnBind += (sender, args) {
    int colIndex = args.ColumnIndex;
    Type type = args.TargetType;
    MemberInfo member = // ... Entirely up to you to define logic
    args.Member = member;
};

上述内容完全是理论上的,但它能满足您的需求吗?

注意:如果未订阅ColumnBind,则只会正常进行正常的基本名称映射。想法?

(注意:它也可以是每个类型的单个事件调用,而不是每列;这可能更有效,因为订阅者将更少地调用GetProperties等等。