AutoMapper10.0或AutoMapper.Data5.0现在在AddDataReaderMapping调用时抛出InvalidOperationException。
创始人
2024-09-23 10:31:26
0

这个问题是因为在 AutoMapper 或 AutoMapper.Data 中,AddDataReaderMapping 方法现在需要提供一个 TypeMapConfiguration 对象。下面是一个示例:

using AutoMapper;
using AutoMapper.Configuration.Conventions;
using AutoMapper.Data;
using Dapper;
using System.Data.Common;

public class MyProfile : Profile
{
    public MyProfile()
    {
        var mapperConfiguration = new MapperConfigurationExpression();
        mapperConfiguration.AddDataReaderMapping();

        CreateMap()
            .ConvertUsing(r =>
            {
                var myType = new MyType();
                myType.Id = r.GetInt32(r.GetOrdinal("Id"));
                myType.Name = r.GetString(r.GetOrdinal("Name"));
                return myType;
            });

        CreateMap()
            .ConvertUsing(t => new[]
            {
                new SqlParameter("Id", t.Id),
                new SqlParameter("Name", t.Name),
            });

        CreateMap()
            .ForMember(x => x["Id"].Value, opt => opt.MapFrom(t => t.Id))
            .ForMember(x => x["Name"].Value, opt => opt.MapFrom(t => t.Name));

        Register(mapperConfiguration);
    }
}

public class MyType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var configuration = new ConfigurationBuilder()
            .AddMaps(typeof(MyProfile).Assembly)
            .AddMapperDataTypes()
            .Build();

        using (var connection = new NpgsqlConnection("Connection String"))
        {
            connection.Open();

            var command = connection.CreateCommand();
            command.CommandText = "SELECT * FROM MyTable;";
            var reader = command.ExecuteReader();

            while (reader.Read())
            {
                var myType = Mapper.Map(reader);
                Console.WriteLine($"ID: {myType.Id}, Name: {myType.Name}");
            }
        }
    }
}

相关内容

热门资讯

教程攻略"福建十三水... 您好:这款福建十三水辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款福建十三水辅助游戏中打牌都会发...
新手必备"天蝎辅助&... 新手必备"天蝎辅助"开挂(透视)wepoker轻量版辅助(有挂技术)wepoker轻量版辅助ai黑科...
玩家必看教程"斗棋联... 玩家必看教程"斗棋联盟俱乐部辅助挂"开挂(透视)hhpoker怎么防作弊(有挂攻略)1、下载安装好斗...
透视计算"开心酷跑破... 开心酷跑破解版 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
总算了解"白银胡乐亲... 白银胡乐亲友房开挂教程视频分享装挂详细步骤在当今的网络游戏中,白银胡乐亲友房作为一种经典的娱乐方式,...
记者爆料"玄龙辅助工... 您好:wepoker透视底牌脚本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
实操分享"葫芦娃七子... 实操分享"葫芦娃七子降妖破解版内购"开挂(透视)德扑圈透视(讲解有挂)1、下载安装好葫芦娃七子降妖破...
一分钟教会你"宝宝浙... 一分钟教会你"宝宝浙江游戏有没有辅助器"开挂(透视)wpk俱乐部辅助器(有挂解惑);无需打开直接搜索...
交流学习经验"多乐够... 交流学习经验"多乐够级辅助软件"开挂(透视)we poker游戏下(有挂技巧)《详细加薇136704...
大神推荐"打两圈兴化... 大神推荐"打两圈兴化辅助"开挂(透视)we-poker靠谱吗(有挂头条)1、下载安装好打两圈兴化辅助...