AutoMapper / AutoMapper.Collection.EFCore

EFCore support for AutoMapper.Collections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoMapper.Collection.EFCore

albertolo opened this issue · comments

Hi,

I'm trying to use AutoMapper.Collection.EFCore, in a asp. net core 3 web api.

Does this library works with .net core 3?

If it I added the documentation code to my method ConfigureServices in my Startup

services
    .AddEntityFrameworkInMemoryDatabase()
    .AddDbContext<InventarioDbContext>();

         services.AddAutoMapper((serviceProvider, automapper) =>
         {
            automapper.AddProfile(typeof(InventarioMappingProfile));
            automapper.AddCollectionMappers();
            automapper.UseEntityFrameworkCoreModel<InventarioDbContext>(serviceProvider);
         }, typeof(InventarioDbContext).Assembly);

 var serviceProvider = services.BuildServiceProvider();

Is this the right way of doing it?

The test is using efcore2 but I expect that it should work.

In your example you are configure two different dbcontext, will it work better if you use the same dbcontext for both the efcore configuration and automapper? What exception you are getting?

The test is using efcore2 but I expect that it should work.

In your example you are configure two different dbcontext, will it work better if you use the same dbcontext for both the efcore configuration and automapper? What exception you are getting?

Sorry, it's the same I already update the issue. I tried the following code, and It finally work my mappings, I will now try to use the Colletion Mappers.

     services.AddAutoMapper(typeof(InventarioMappingProfile)); //original
     services.AddAutoMapper((serviceProvider, automapper) =>
     {
        automapper.AddCollectionMappers();
        automapper.UseEntityFrameworkCoreModel<InventarioDbContext>(serviceProvider);
     }, typeof(InventarioDbContext).Assembly);

Question on using mapping profiles

Since using AutoMapper.Collection and AutoMapper.Collection.EntityFramework in ASP.NET Core requires you to set up the mapping configuration in a different way, do we need to get rid of the initial mapping profile we created and use automapper.CreateMap<> inside the IMapperConfigurationExpression for all mapping, or everything should work as far as mapping collections (including child properties of an entity that are collections) after adding this in Program.cs:

services.AddAutoMapper((serviceProvider, automapper) =>
{        
  automapper.AddCollectionMappers();
  automapper.UseEntityFrameworkCoreModel<DbContext>(serviceProvider);
}, typeofDbContext).Assembly);

That works fine, every map inside there is created under a default profile.