AutoMapper / AutoMapper.Collection

AutoMapper support for updating existing collections by equivalency
MIT License
245 stars 59 forks source link

GenerateEntityFrameworkPrimaryKeyPropertyMaps throws null pointer exception #100

Closed jwstevens-ii closed 2 years ago

jwstevens-ii commented 6 years ago

I've added AutoMapper.Collection and AutoMapper.Collection.EntityFramework to my project. But I'm getting a null pointer exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

My code is:

var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddCollectionMappers();
    cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<PlaygroundContext>>();
...
}

All of my entities have ID properties and are attributed with [Key]

Stack Trace:
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.<>c.<CreateEquivalentExpression>b__11_1(PropertyMap pm)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.CreateEquivalentExpression(IEnumerable`1 propertyMaps)
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.<>c__DisplayClass7_0.<GetEquivalentExpression>b__1(IGeneratePropertyMaps _)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.<>c__DisplayClass7_0.<GetEquivalentExpression>b__0(TypePair tp)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.GetEquivalentExpression(IConfigurationProvider configurationProvider, TypeMap typeMap)
   at AutoMapper.EquivalencyExpression.EquivalentExpressions.GetEquivalentExpression(IConfigurationObjectMapper mapper, Type sourceType, Type destinationType)
   at AutoMapper.Mappers.EquivalentExpressionAddRemoveCollectionMapper.IsMatch(TypePair typePair)
   at AutoMapper.MapperConfiguration.<>c__DisplayClass69_0.<FindMapper>b__0(IObjectMapper m)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at AutoMapper.MapperConfiguration.FindMapper(TypePair types)
   at AutoMapper.MapperConfiguration.GetTypeMap(TypePair initialTypes)
   at AutoMapper.LockingConcurrentDictionary`2.<>c__DisplayClass2_1.<.ctor>b__1()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at AutoMapper.MapperConfiguration.ResolveTypeMap(TypePair typePair, ITypeMapConfiguration inlineConfiguration)
   at AutoMapper.MapperConfiguration.ResolveTypeMap(TypePair typePair)
   at AutoMapper.Execution.TypeMapPlanBuilder.<>c__DisplayClass17_0.<CheckForCycles>g__ResolveTypeMap|6(TypePair types)
   at AutoMapper.Execution.TypeMapPlanBuilder.<>c__DisplayClass17_0.<CheckForCycles>g__ResolvePropertyTypeMap|5(PropertyMap propertyMap)
   at AutoMapper.Execution.TypeMapPlanBuilder.<>c__DisplayClass17_0.<CheckForCycles>b__1(PropertyMap pm)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at AutoMapper.Execution.TypeMapPlanBuilder.CheckForCycles(HashSet`1 typeMapsPath)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda(HashSet`1 typeMapsPath)
   at AutoMapper.TypeMap.Seal(IConfigurationProvider configurationProvider)
   at AutoMapper.MapperConfiguration.Seal()
   at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
   at AutoMapper.MapperConfiguration..ctor(Action`1 configure)
TylerCarlson1 commented 6 years ago

You might not be mapping all your Primary Keys to properties on your Model. All primary keys need to be mapped to the model, so that they can figure out if they are equal to each other by a unique identifier.

If that's not the case can you provide a gist of it failing so I can replicate what's going wrong?

Tasteful commented 6 years ago

Can be related to #71

Tasteful commented 6 years ago

@jwstevensii Please test with the package that was released of AutoMapper.Collection to see if your error if fixed.

wakm commented 5 years ago

hi, we got the same issue and it was as @TylerCarlson1 stated, an issue with our entity mapping to our contracts. Is it possible to improve the codebase to throw a more understandable exception at this point?

Xriuk commented 2 years ago

Hi, I get the same problem here, we use Owned entities (which don't have keys) and some entities have also composite primary keys, I don't know if this might be a problem...

builder.Services.AddAutoMapper((serviceProvider, config) => {
  config.AllowNullCollections = true;
  config.AddCollectionMappers();
  config.UseEntityFrameworkCoreModel<DB>(serviceProvider);
}, Assembly.GetExecutingAssembly());

immagine immagine