Does this work with `AutoMapper.Extensions.Microsoft.DependencyInjection`?
See original GitHub issueHi Jimmy,
Is there a way to setup the GenerateEntityFrameworkCorePrimaryKeyPropertyMaps
when using AutoMapper.Extensions.Microsoft.DependencyInjection
? From what I can see, GenerateEntityFrameworkCorePrimaryKeyPropertyMaps
needs an IServiceProvider, which is not available in ConfigureServices
without registering the service using the implementation factory overload of one of the Add calls on IServiceCollection
.
Ideally, I would like to be able to do something along the lines of:
services.AddAutoMapper(mapper =>
{
mapper.AddCollectionMappers();
mapper.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<MyContext>>();
});
but there appears to be a chicken and egg problem here. From what I can tell, SetGeneratePropertyMaps
immediately creates an instance of the property map generator and attempts to build the mappings rather than doing it after the container has been built or when IMapper
is resolved.
Thanks! 😃
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (13 by maintainers)
One option is to pass the
IServiceProvider
when theAddAutoMapper
is building it’s config.If we add an overload to the existing
that also will send in the
IServiceProvider
in the config method like thisthen we will have the
IServiceProvider
directly and can use that when register the EFCore mappings direclyMS is doing this for configuring EFCore itself https://github.com/aspnet/EntityFrameworkCore/blob/c07c2cef92a30e1f8272db0395756e836e47d864/src/EFCore/EntityFrameworkServiceCollectionExtensions.cs#L216
I see, the error is confusing but it should be replaced with:
cfg.UseEntityFrameworkCoreModel<MyDbContext>(services);
instead of:
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<MyDbContext>>();