使用AutoMapper Profile Instance时遇错误,请求技术排查
AutoMapper Profile配置错误排查与解决
问题原因
你的代码中命名空间CashCraftApp.Profile与AutoMapper提供的Profile基类名称冲突,编译器无法正确识别你要继承的是AutoMapper的Profile类,导致编译错误。
解决方案
方案1:重命名命名空间(推荐)
将命名空间改为非冲突名称,比如复数形式CashCraftApp.Profiles:
using System; using AutoMapper; using CashCraftApp.Model; namespace CashCraftApp.Profiles { public class AutoMapperProfile : Profile { public AutoMapperProfile() { CreateMap<RegisterNewAccountModel, Account>(); } } }
方案2:使用完全限定名引用AutoMapper.Profile
如果不想修改命名空间,在继承时明确指定AutoMapper的Profile类:
using System; using AutoMapper; using CashCraftApp.Model; namespace CashCraftApp.Profile { public class AutoMapperProfile : AutoMapper.Profile { public AutoMapperProfile() { CreateMap<RegisterNewAccountModel, Account>(); } } }
额外验证:确认AutoMapper服务注册
确保在项目的服务配置中正确注册AutoMapper(以.NET 6+为例):
// Program.cs builder.Services.AddAutoMapper(typeof(AutoMapperProfile));
内容的提问来源于stack exchange,提问作者Gbenga B Ayannuga
相关产品推荐
相关产品推荐

