Search code examples
odataautomapper

Serialization issue using Automapper and odata


I am attempting to map an entity to a model like this

    public static AccountViewModel ToModel(this Account entity)
    {
        return Mapper.Map<Account, AccountViewModel>(entity);
    }

This is where I use this extension method

    [EnableQuery]
    public IQueryable<AccountViewModel> GetAccounts()
    {
        return _accountRepository.Table
            .Select(x => x.ToModel()).AsQueryable();
    }

I get the following exception

LINQ to Entities does not recognize the method 'Api.ViewModels.AccountViewModel ToModel(Data.Models.Account)' method, and this method cannot be translated into a store expression.


Solution

  • Just need to do this

        [EnableQuery]
        public IQueryable<AccountViewModel> GetAccounts()
        {
            return _accountRepository.Table
                .ToList()
                .Select(x => x.ToModel()).AsQueryable();
        }