2

I am trying to map a model to a view, but I receive the error above when I am trying to display all my elements, since Automapper doesn't recognize the IEnumerable I think. I receive the error when I am trying to map FixedAssets to FixedAssetsView and FixedAssetsView to FixedAssets.

Here are the objects I am trying to map:

FixedAssets

public class FixedAssets : IEntityBase
{
    public int ID { get; set; }
    public string name { get; set; }
    public virtual ICollection<Category> category { get; set; }
    public string serialNo { get; set; }
    public string provider { get; set; 
    public DateTime acquisitionDate { get; set; }
    public DateTime warrantyEnd { get; set; }
    public int inventoryNo { get; set; }
    public string allocationStatus { get; set; }
    public string owner { get; set;  }
    public DateTime allocationDate { get; set; }
    public string serviceStatus { get; set; }
    public string serviceResolution { get; set; }

    public FixedAssets()
    {
        this.category = new HashSet<Category>();
    }
}

FixedAssetsView

public class FixedAssetsView
{
    public int ID { get; set; }
    public string name { get; set; }
    public virtual ICollection<CategoryView> category { get; set; }
    public string serialNo { get; set; }
    public string provider { get; set; }
    public DateTime acquisitionDate { get; set; }
    public DateTime warrantyEnd { get; set; }
    public int inventoryNo { get; set; }
    public string allocationStatus { get; set; }
    public string owner { get; set; }
    public DateTime allocationDate { get; set; }
    public string serviceStatus { get; set; }
    public string serviceResolution { get; set; }
}

Category

public class Category : IEntityBase
{
    public int ID { get; set; }
    public string categoryName { get; set; }
    public virtual ICollection<FixedAssets> fixedasset { get; set; }

    public Category()
    {
        this.fixedasset = new HashSet<FixedAssets>();
    }
}

CategoryView

public class CategoryView
{
    public int ID { get; set; }
    public string categoryName { get; set; }

    public virtual ICollection<FixedAssetsView> fixedasset { get; set; }
}

Automapper configuration

Mapper.Initialize(x =>
        {
            x.CreateMap<FixedAssets, FixedAssetsView>();
            x.CreateMap<FixedAssetsView, FixedAssets>();

            x.CreateMap<Category, CategoryView>();
            x.CreateMap<CategoryView, Category>();

        });
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
Artyomska
  • 1,309
  • 5
  • 26
  • 53
  • I don't see any error with the latest build from MyGet, but another answer might apply. See [here](https://stackoverflow.com/questions/45298110/how-to-ignore-property-of-property-in-automapper-mapping/45300519#45300519). – Lucian Bargaoanu Aug 03 '17 at 12:42
  • Possible duplicate of [How to ignore property of property in AutoMapper mapping?](https://stackoverflow.com/questions/45298110/how-to-ignore-property-of-property-in-automapper-mapping) – Lucian Bargaoanu Aug 18 '17 at 08:29
  • What's missing here is the code that actually executes the mapping. – Gert Arnold Nov 28 '18 at 13:11
  • This error happens when there is a problem with your mapping. The cause is hidden in the exception message that only shows: Error mapping types. Mapping types: IEnumerable1 -> IEnumerable1 To find the cause, try to make AutoMapper map only one item of your collection. If you get an exception, the exception message might contain a better explanation of what is wrong. – Totalys Mar 07 '20 at 01:03

1 Answers1

-1

I believe you need a .ForMember in your Mapper initialization.

eg:

Mapper.CreateMap<IEnumerable<Source>, IEnumerable<Target>>()
.ForMember(f => f, mp => mp.MapFrom(
                                mfrom => mfrom.Select(s => AutoMapper.Mapper.Map(s, new Target())
                            )
          );
Alen Zarkovic
  • 224
  • 2
  • 11