You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 13:54:51