AutoMapper使用中的问题

指定值只会执行一次

 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
public class MomanBaseProfile : Profile
  {
      public MomanBaseProfile()
      {
          CreateMap<RequestBase, MomanEntity>()
              .ForMember(d => d.ID, op => op.Ignore())
              .ForMember(d => d.UID, op => op.UseValue(Guid.NewGuid()))//使用Mapper过程中始终不变
              .ForMember(d => d.CreateTime, op => op.UseValue(DateTime.UtcNow))
              .ForMember(d => d.UpdateTime, op => op.UseValue(DateTime.UtcNow));
 
          CreateMap<RequestWithUserBase, MomanEntity>()
              .IncludeBase<RequestBase, MomanEntity>();
      }
  }

 

继承类如何减少Map问题解决

复制代码
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
public class MomanBaseProfile : Profile
    {
        public MomanBaseProfile()
        {
            CreateMap<RequestBase, MomanEntity>()
                .ForMember(d => d.ID, op => op.Ignore())
                .ForMember(d => d.UID, x => x.MapFrom(ax => Guid.NewGuid()))
                .ForMember(d => d.CreateTime, x => x.MapFrom(ax => DateTime.UtcNow))
                .ForMember(d => d.UpdateTime, x => x.MapFrom(ax => DateTime.UtcNow));
 
           //继承类两种方式,一种使用IncludeBase(从父类映射继承)如下,另一中使用Include(从子类映射继承)
            CreateMap<RequestWithUserBase, MomanEntity>()
                .IncludeBase<RequestBase, MomanEntity>();
        }
    }
 

参考

复制代码

AutoMapper: Why is UseValue only executed once

posted @   霍旭东  阅读(1441)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示