首页 新闻 赞助 找找看

asp.net core中

0
悬赏园豆:30 [已关闭问题] 关闭于 2018-09-27 17:20

项目在正式环境运行一段时间之后
就会报下面类似问题
Unable to cast object of type 'lm.Erp.Contracts.Dto.ResultDto' to type 'lm.Erp.Contracts.Dto.PaginationResultDto1[lm.Erp.Contracts.Dto.KeyValueDto2[System.Int64,System.Int32]]'.,
Unable to cast object of type 'lm.Erp.Contracts.Dto.ResultDto' to type 'lm.Erp.Contracts.Dto.ResultDto`1[lm.Erp.Contracts.Dto.Sys.LoginUserDto]'.,

使用的了AutoMapper、Dapper
一开始,或者每次项目重启都没问题,抛出以上类似问题,都是在项目运行一段时间之后

豆的努力时代的主页 豆的努力时代 | 初学一级 | 园豆:183
提问于:2018-09-13 18:00
< >
分享
所有回答(1)
0

1、asp.net core2.0中 使用AutoMapper 6.2.2,AutoMapper.Extensions.Microsoft.DependencyInjection 3.2.0
2、在Startuup.cs中 ConfigureServices(IServiceCollection services) 方法中注册:services.AddAutoMapper()
3、新建MappingProfile类 继承Profile
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<PaginationResultModel<SysUserModel>, PaginationResultDto<SysUserDto>>(); //例如
}
}
4、直接Mapper.Map<TSource, TDest>(s)使用了
对于list中list类,一定要第三布的CreateMap
题外话:这是自己封装的automapper,不嫌弃可以拿去用
public class AutoMapHelper
{
public static List<TDest> Map<TSource, TDest>(List<TSource> sl, Func<TDest, TDest> func = null)
{
var list = new List<TDest>();

        if (null == sl)
        {
            return list;
        }
        foreach (var item in sl)
        {
            var info = Mapper.Instance.Map<TSource, TDest>(item);
            if (func != null)
            {
                info = func(info);
            }

            list.Add(info);
        }

        return list;
    }

    public static IList<TDest> Map<TSource, TDest>(IList<TSource> sl, Func<TDest, TDest> func = null)
    {
        var list = new Collection<TDest>();

        if (null == sl)
        {
            return list;
        }
        foreach (var item in sl)
        {
            var info = Mapper.Instance.Map<TSource, TDest>(item);
            if (func != null)
            {
                info = func(info);
            }

            list.Add(info);
        }

        return list;
    }

    public static IList<TDest> Map<TSource, TDest>(IList<TSource> sl, Action<TSource, TDest> action)
    {
        var list = new Collection<TDest>();

        if (null == sl)
        {
            return list;
        }
        foreach (var item in sl)
        {
            var info = Mapper.Instance.Map<TSource, TDest>(item);

            action?.Invoke(item, info);

            list.Add(info);
        }

        return list;
    }

    public static List<TDest> Map<TSource, TDest>(List<TSource> s1, Action<TSource, TDest> action)
    {
        var list = new List<TDest>();
        if (null == s1)
        {
            return list;
        }
        foreach (var item in s1)
        {
            var info = Mapper.Instance.Map<TSource, TDest>(item);
            action?.Invoke(item, info);
            list.Add(info);
        }

        return list;
    }

    public static TDest Map<TSource, TDest>(TSource s)
    {
        return Map<TSource, TDest>(s, null);
    }

    public static TDest Map<TSource, TDest>(TSource s, Action<TSource, TDest> action)
    {
        try
        {
            var result = Mapper.Map<TSource, TDest>(s);
            action?.Invoke(s, result);
            return result;
        }
        catch (Exception ex)
        {
            var t = ex.Message;

            var result = Mapper.Map<TSource, TDest>(s);
            action?.Invoke(s, result);
            return result;
        }
    }
}
豆的努力时代 | 园豆:183 (初学一级) | 2018-09-27 17:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册