question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does this work with `AutoMapper.Extensions.Microsoft.DependencyInjection`?

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Tastefulcommented, Aug 11, 2018

One option is to pass the IServiceProvider when the AddAutoMapper is building it’s config.

If we add an overload to the existing

public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<IMapperConfigurationExpression> additionalInitAction)

that also will send in the IServiceProvider in the config method like this

public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<IServiceProvider, IMapperConfigurationExpression> additionalInitAction)

then we will have the IServiceProvider directly and can use that when register the EFCore mappings direcly

services.AddAutoMapper((serviceProvider, cfg) => {
    cfg.AddCollectionMappers();
    cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<MyContext>>(serviceProvider);
});

MS is doing this for configuring EFCore itself https://github.com/aspnet/EntityFrameworkCore/blob/c07c2cef92a30e1f8272db0395756e836e47d864/src/EFCore/EntityFrameworkServiceCollectionExtensions.cs#L216

0reactions
replaysMikecommented, Dec 6, 2018

I see, the error is confusing but it should be replaced with:

cfg.UseEntityFrameworkCoreModel<MyDbContext>(services);

instead of:

cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<MyDbContext>>();

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoMapper.Extensions.Microsoft.DependencyInjection ...
adds profiles to mapping configuration; adds value resolvers, member value resolvers, type converters to the container. To use, with an IServiceCollection ...
Read more >
AutoMapper/AutoMapper.Extensions.Microsoft. ...
This package and repository have been deprecated and archived. Its code is folded in to the main AutoMapper repository.
Read more >
What is the difference between automapper.extensions. ...
The NuGet package AutoMapper.Extensions.Microsoft.DependencyInjection will also load the dependend packages (listed under dependencies) into ...
Read more >
Releases · AutoMapper/AutoMapper.Extensions.Microsoft. ...
This release supports AutoMapper 8.1.0. It includes a couple new features: Changing default Mapper service lifetime to Transient ...
Read more >
How to use AutoMapper on ASP.NET Core 3.1 via ... - Nicky Liu
Step 1. Install AutoMapper extension from Package Manager in your project. Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 7.0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found