Kanasi
Kanasi

Reputation: 95

Incorrect number of arguments supplied for call to method

I'm using the last version of Automapper to map a model from the database that was created automatically via EF Core with my own model that has the same structure.

EF Core model :

public partial class Language
{
    public int Id { get; set; }

    public string FullName { get; set; } = null!;
    public string Abbreviation { get; set; } = null!;
    public string Description { get; set; } = null!;
    public string DateFormat { get; set; } = null!;
    public string TimeFormat { get; set; } = null!;

    public virtual ICollection<About> Abouts { get; set; } = new List<About>();
    public virtual ICollection<Contact> Contacts { get; set; } = new List<Contact>();
    public virtual ICollection<PostsTranslation> PostsTranslations { get; set; } = new List<PostsTranslation>();
}

My own model:

public class Language
{
    public int Id { get; set; }

    public string FullName { get; set; } = null!;
    public string Abbreviation { get; set; } = null!;
    public string Description { get; set; } = null!;

    public string DateFormat { get; set; } = null!;
    public string TimeFormat { get; set; } = null!;
}

But I have get this error:

Incorrect number of arguments supplied for call to method 'DatabaseProvider.Models.Language get_Item(Int32)' (Parameter 'property')

Automapper code:

public class MapperConfig
{
    public static Mapper InitializeAutomapper()
    {
        //Provide all the Mapping Configuration
        var config = new MapperConfiguration(cfg =>
        {
cfg.CreateMap<API.Language, DB.Language>()
    .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
    .ForMember(d => d.FullName, opt => opt.MapFrom(s => s.FullName))
    .ForMember(d => d.Abbreviation, opt => opt.MapFrom(s => s.Abbreviation))
    .ForMember(d => d.Description, opt => opt.MapFrom(s => s.Description))
    .ForMember(d => d.DateFormat, opt => opt.MapFrom(s => s.DateFormat))
    .ForMember(d => d.TimeFormat, opt => opt.MapFrom(s => s.TimeFormat));

cfg.CreateMap<List<API.Language>, List<DB.Language>>();

cfg.CreateMap<DB.Language, API.Language>()
    .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
    .ForMember(d => d.FullName, opt => opt.MapFrom(s => s.FullName))
    .ForMember(d => d.Abbreviation, opt => opt.MapFrom(s => s.Abbreviation))
    .ForMember(d => d.Description, opt => opt.MapFrom(s => s.Description))
    .ForMember(d => d.DateFormat, opt => opt.MapFrom(s => s.DateFormat))
    .ForMember(d => d.TimeFormat, opt => opt.MapFrom(s => s.TimeFormat));

cfg.CreateMap<List<DB.Language>, List<API.Language>>();
        });
        //Create an Instance of Mapper and return that Instance
        var mapper = new Mapper(config);
        return mapper;
       }
    }
}

Calling code:

private readonly Mapper _mapper;
private readonly DatabaseConnector _connector = new();
private readonly ILogger<DatabaseController> _logger;

public DatabaseController(ILogger<DatabaseController> logger)
{
    _mapper = MapperConfig.InitializeAutomapper();
    _connector = new DatabaseConnector();
    _logger = logger;
}

[HttpGet("GetLanguages")]
public IEnumerable<Language> GetLanguages()
{
    var dbList = _connector.Get<DB.Language>();
    var list = _mapper.Map<List<DB.Language>, List<Language>>(dbList);

    return list;
}

What should I do?

Upvotes: -2

Views: 2808

Answers (1)

Kanasi
Kanasi

Reputation: 95

I found the solution. If u have problem with "Item" or with "name" properties, just add this lines to the configuration of your mapper:

cfg.AllowNullCollections = true;
cfg.AddGlobalIgnore("Item");

It will fix your problems. And remember! Don't map lists, map only single items between each other!

Upvotes: 3

Related Questions