Automapper missing type map configuration or unsupported mapping - Error

前端 未结 12 1396
旧时难觅i
旧时难觅i 2020-11-27 16:25

Entity Model

public partial class Categoies
{
    public Categoies()
    {
        this.Posts = new HashSet();
    }

    public int Id { get; se         


        
相关标签:
12条回答
  • 2020-11-27 17:02

    In your class AutoMapper profile, you need to create a map for your entity and viewmodel.

    ViewModel To Domain Model Mappings:

    This is usually in AutoMapper/DomainToViewModelMappingProfile

    In Configure(), add a line like

    Mapper.CreateMap<YourEntityViewModel, YourEntity>();
    

    Domain Model To ViewModel Mappings:

    In ViewModelToDomainMappingProfile, add:

    Mapper.CreateMap<YourEntity, YourEntityViewModel>();
    

    Gist example

    0 讨论(0)
  • 2020-11-27 17:03

    I did this to remove the error:

    Mapper.CreateMap<FacebookUser, ProspectModel>();
    prospect = Mapper.Map(prospectFromDb, prospect);
    
    0 讨论(0)
  • 2020-11-27 17:05

    Where have you specified the mapping code (CreateMap)? Reference: Where do I configure AutoMapper?

    If you're using the static Mapper method, configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications.

    If the configuration isn't registered before calling the Map method, you will receive Missing type map configuration or unsupported mapping.

    0 讨论(0)
  • 2020-11-27 17:06

    Notice the Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D class in the exception? That's an Entity Framework proxy. I would recommend you disposing of your EF context to ensure that all your objects are eagerly loaded from the database and no such proxies exist:

    [HttpPost]
    public ActionResult _EditCategory(CategoriesViewModel viewModel)
    {
        Categoies category = null;
        using (var ctx = new MyentityFrameworkContext())
        {
            category = ctx.Categoies.Find(viewModel.Id);
        }
        AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category);
        //category = AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category);
        entity.SaveChanges();
    }
    

    If the entity retrieval is performed inside a data access layer (which of course is the correct way) make sure you dispose your EF context before returning instances from your DAL.

    0 讨论(0)
  • 2020-11-27 17:07

    I know this is a rather old question as of now, but I had found the proper solution was that I was not declaring the assembly attribute.

    My code is:

    using AutoMapper;
    ...
    
    namespace [...].Controllers
    {
        public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel>
        {
            Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap();
        }
        ...
    }
    

    This was fixed by adding the following line before my namespace declaration:

    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")]
    

    The full code is:

    using AutoMapper;
    ...
    
    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")]
    
    namespace [...].Controllers
    {
        public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel>
        {
            Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap();
        }
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 17:08

    I had same issue in .Net Core. Because my base dto class(i give it as a type in startup for automapper assembly) was in different project. Automapper tried to search for profiles in base class project. But my dto's were in different project. I moved my base class. And problem solved. This may help for some persons.

    0 讨论(0)
提交回复
热议问题