Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping class array using AutoMapper

I'm new to AutoMapper and trying to map Array class ItemLink[].

public class ViewModel
{
  public ItemLink[] ItemLinks { get; set; }
}

public class ItemLink
{
  public string Description { get; set; }
}

I tried:

Mapper.Map<viewModel.ItemLink>(db.ItemLinks);

Error:

"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."

Can't it be simple mapping?

EDIT 1

To clarify more, I'm getting similar class structure from database. Example,

public class Db
{
  public ItemLink[] ItemLinks { get; set; }
}

So, I want to map ViewModel.ItemLink[] with Db.ItemLink[].

like image 503
Ashwini Verma Avatar asked Dec 06 '25 15:12

Ashwini Verma


2 Answers

You cannot provide variable to a generic parameter like you do in Mapper.Map<viewModel.ItemLink>(db.ItemLinks);. It is called Type parameter and must be a type.

As @gisek mentioned in his answer you need to configure mapper first. Normally it is done at application startup.

You can consider to fetch full objects from db, but you also have an option to use Queryable Extensions which are there to only fetch data you need for your view model.

The configuration. I assume that you have namespace DB for entity in database, and View namespace for view model.

Mapper.Initialize(cfg => {
    cfg.CreateMap<DB.ItemLink, View.ItemLink>();
});
Mapper.Configuration.AssertConfigurationIsValid();

Fetch full entity from DB and then map it to property:

var viewModel = new View.Item();
viewModel.ItemLinks = Mapper.Map<View.ItemLink[]>(db.ItemLinks.ToArray());

Project entity from DB to view model:

var viewModel = new View.Item();
viewModel.ItemLinks = db.ItemLinks.ProjectTo<View.ItemLink>().ToArray();
like image 122
Andrii Litvinov Avatar answered Dec 09 '25 05:12

Andrii Litvinov


I assumed you are using .net mvc

Firstly you need Initialize your mapping on Application Start, there is no need to initialize every time in your controller etc.

Following error means you didn't initialize mapper, automapper doesn't know your source and destination object.

Error:

"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."

For Solution:

Create an AutoMapperConfig object in your App_Start folder.

public class AutoMapperConfig
{
    public static void Register()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<source,destination>(); /*If source and destination object have same propery */
            cfg.CreateMap<source, destination>()
             .ForMember(dest => dest.dId, opt => opt.MapFrom(src => src.sId)); /*If source and destination object haven't same property, you need do define which property refers to source property */ 
         });
    }
}

In your Global.asax.cs

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        AutoMapperConfig.Register(); // Call Register method on App_Start
    }

In your controller

   var mappedArray = Mapper.Map<viewmodel.ItemLink[]>(db.ItemLinks);

or

var mappedArray = Mapper.Map<viewmodel.ItemLink[],ItemLinks[]>(db.ItemLinks); //ItemLinks[] refers to your dto.
like image 32
fatih.aslantas Avatar answered Dec 09 '25 04:12

fatih.aslantas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!