Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProjectToType doesn't work with DI(MapContext) #266

Closed
sonsy-soft opened this issue Sep 1, 2020 · 2 comments
Closed

ProjectToType doesn't work with DI(MapContext) #266

sonsy-soft opened this issue Sep 1, 2020 · 2 comments

Comments

@sonsy-soft
Copy link

I have this Config

public void Register(TypeAdapterConfig config)
        {
            config
                .NewConfig<Domain.Entity.News.News, SitemapNode>()
                .AddDestinationTransform((string x) => x.Trim())
                .Map(dest => dest.Url,                   src => GetNewsUrl(src.Id,src.Title))
                .Map(dest => dest.LastModified,   src => src.ModifiedDate == null ? src.CreatedDate:src.ModifiedDate.Value)
              ;
        }

        private string GetNewsUrl(long ID, string TItle)
        {
            var HttpContextAccessor = MapContext.Current.GetService<IHttpContextAccessor>();
            return "Fine";          
        }

when I want to run the below code I got exception :

await
                  _Context
                   .Set<Domain.Entity.News.News>()
                    .AsNoTracking()
                    .ProjectToType<SitemapNode>(_mapper.Config)
                    .ToListAsync(cancellationToken)
                    .ConfigureAwait(false);

InvalidOperationException: Mapping must be called using ServiceAdapter

@chaowlert
Copy link
Collaborator

Yeah, this is a bit complicated issue. ProjectToType is for translate mapping into expression. But MapContext required context for actual mapping. I will find the way to solve this in the future. For now please, mapping using _mapper.Map.

var news = await _context.Set<News>().ToListAsync();
var dtos = _mapper.Map<SiteMapNode>(news);

@sonsy-soft
Copy link
Author

sonsy-soft commented Sep 3, 2020

cool
but I think

var dtos = _mapper.Map<IEnumrable<SiteMapNode>>(news);

is true and we have same exception in the mapping
but
I use this way to solve it

_context
 .Set<Domain.Entity.News.News>()
.AsNoTracking()
.ToList()
.Select(x=> _mapper.Map<SitemapNode>(x));

thanks chaowlert .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants