0
votes

I'm trying to map with AutoMapper

I have this model from data:

public partial class ModelFromData
{
    public int Id { get; set; }
    public int LastPosition { get; set; }

    public virtual SomeModel SomeModel{ get; set; }
}

And my view Model

public class ViewModel
{
    public int Id { get; set; }
    public int LastPosition { get; set; }

    public virtual SomeModel SomeModel{ get; set; }
}

My Mapper configures:

public class MapperConfig
{
    public static void InitMaps()
    {
        Mapper.CreateMap<ModelFromData, ViewModel>();
        Mapper.CreateMap<ViewModel, ModelFromData>();

        Mapper.AssertConfigurationIsValid();
    }
}

public static class MapExtensions
{
    public static T To<T>(this Object from)
    {
        return Mapper.Map<T>(from);
    }
}

When I try to AutoMap I get the folowing error:

Missing type map configuration or unsupported mapping.

Mapping types: ModelFromData -> ViewModel

enter code here

Project.Data.ModelFromData -> Project.Web.Models.ViewModel

1
why do you want a viewModel if it is identical? my guess is that it is not identical , that there are nullable types in one of them , or something is not matching up. Did you leave out the parts of the classes that are causing it to break?Scott Selby
My Controller call the mapper like this: IEnumerable<ViewModel> model = service.GetAll().To<IEnumerable<ViewModel>>();Mmmcc
My EF requests are returning a type called Proxie. Maybe because that proxie Im not getting automap success? System.Data.Entity.DynamicProxies .Model_5E43C6C196972BF0754973E48C9C941092D86818CD94005E9A759B70BF6E48E6Mmmcc

1 Answers

0
votes

This is the error you will get when trying to map an object before the "CreateMap" is called. What framework are you using? I would hazard a guess at Asp.Net?

If so, Call your MapperConfig.InitMaps(); method from global.asax. Good answer here: https://stackoverflow.com/a/6825394/692830