AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

GetQueryAsync / ProjectTo not working with "UseInMemoryDatabase" db context #185

Closed Edgaras91 closed 1 year ago

Edgaras91 commented 1 year ago

Is this a known issue, as I've seen mentions in stack overflow?

In tests (unit) when creating WebApplicationFactory and overriding DbContexts to be in memory, the GetQueryAsync stops working.

Source/destination types

                //When using in-memory database only:
                var hasResults = await _accountsDbContext.AccountAddresses.ToListAsync(cancellationToken: cancellationToken);
                var noResults0 = await _accountsDbContext.AccountAddresses.ProjectTo<AccountAddressDto>(_mapper.ConfigurationProvider).ToListAsync(cancellationToken: cancellationToken);
                var noResults1 = await (await _accountsDbContext.AccountAddresses.GetQueryAsync(_mapper, request.QueryOptions)).ToListAsync(cancellationToken: cancellationToken);
                var noResults2 = await _accountsDbContext.AccountAddresses.GetQueryAsync(_mapper, request.QueryOptions);

//SETUP:

 var accountsDescriptor = serviceCollection.Single(
                            d => d.ServiceType ==
                                 typeof(DbContextOptions<AccountsDbContext>));

                        serviceCollection.Remove(accountsDescriptor);

                        serviceCollection.AddDbContext<AccountsDbContext>(options =>
                            options.UseInMemoryDatabase("AccountsDB"));

Mapping configuration

     public void Mapping(Profile profile)
        {
            profile.CreateMap<AccountAddress, AccountAddressDto>()
                .ForMember(d => d.Title, s => s.MapFrom(x => x.Title.TitleName));
        }

Version: x.y.z

ASP NET6 AutoMapper.AspNetCore.OData.EFCore 4.0.0 Microsoft.EntityFrameworkCore.SqlServer 6.0.13

May be related to this: https://stackoverflow.com/questions/60837497/automapper-projectto-not-working-with-efcore-in-memory-database-unit-tests

Edgaras91 commented 1 year ago

Ignore, this was not working on newly added Integration tests, because child tables were not "seeded", so the auto mapper profile could not map to child data, whereas using a real database, already had "good" data with all of it's child tables populated.