NetCore 使用 AutoMapper

引用Nuget包

AutoMapper
AutoMapper.Extensions.Microsoft.DependencyInjection

 

配置Program.cs

//添加Profile
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

 

配置Profile

public class AutoMapperProfile : Profile
{
    /// <summary>
    /// 实体映射
    /// </summary>
    public AutoMapperProfile()
    {
        #region 接口映射
        //DTO映射到源数据
        CreateMap<InterDto, InterModel>();
        #endregion
    }
}

 

 控制器使用

var model = _mapper.Map<InterModel>(modelDto);

 

参考:https://www.cnblogs.com/myfqm/p/12982203.html

posted @ 2023-07-08 20:32  microsoft-zhcn  阅读(44)  评论(0)    收藏  举报