You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
KeyNotFoundException: The given key 'Id' was not present in the dictionary. #79
Hi!
I'm getting this error randomly when I try to query data from our database using dapper and then convert the result to an object using Slapper.
Code sample:
var data = await _dapperRepository.QueryAsync<dynamic>(sqlQuery, parameters);
var result = Slapper.AutoMapper.MapDynamic<UserData>(data).ToList();
The exception:
System.Collections.Generic.KeyNotFoundException: The given key 'Id' was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Linq.Enumerable.SelectListIterator`2.ToArray() at System.Linq.Enumerable.DefaultIfEmptyIterator`1.ToArray() at Slapper.AutoMapper.InternalHelpers.GetCacheKey(Type type, IDictionary`2 properties, Object parentInstance) at Slapper.AutoMapper.InternalHelpers.GetInstance(Type type, IDictionary`2 properties, Object parentInstance) at Slapper.AutoMapper.InternalHelpers.MapCollection(Type type, IDictionary`2 dictionary, Object instance, Object parentInstance) at Slapper.AutoMapper.InternalHelpers.Map(IDictionary`2 dictionary, Object instance, Object parentInstance) at Slapper.AutoMapper.Map(Type type, IEnumerable`1 listOfProperties, Boolean keepCache) at Slapper.AutoMapper.MapDynamic[T](IEnumerable`1 dynamicListOfProperties, Boolean keepCache)
This error is happening inside this method when trying to Select(id => properties[id]). The Key does not exist in the dictionary and is throwing this exception.
private static InstanceKey GetCacheKey(Type type, IDictionary<string, object> properties, object parentInstance)
{
var identifierValues = GetIdentifiers(type)?.Select(id => properties[id]).DefaultIfEmpty(Guid.NewGuid()).ToArray()
?? new object[] { Guid.NewGuid() };
var key = new InstanceKey(type, identifierValues, parentInstance);
return key;
}
Unfortunately I'm not able to reproduce the error locally and submit a pull request because it happens randomly in our production environment. Never had this issue running locally and debugging.
Does anyone have an idea on what could be the cause of the error?
Hi! I was reading this topic, because I had the same problem here. I just set a unique identifier and it work very well.
Like this: Slapper.AutoMapper.Configuration.AddIdentifier(typeof(MyClass), "MyUniqueIdentifier");
Activity
alxtr1bal commentedon Dec 31, 2020
Same here... unable to pinpoint this problem though...
povillar1985 commentedon Aug 1, 2021
got same issue. any updates here? Thanks
randyburden commentedon Aug 27, 2021
Someone would need to create a test that reliably reproduces the problem in order for anyone to attempt to address this issue.
CrisConter commentedon Nov 17, 2021
Hi! I was reading this topic, because I had the same problem here. I just set a unique identifier and it work very well.
Like this:
Slapper.AutoMapper.Configuration.AddIdentifier(typeof(MyClass), "MyUniqueIdentifier");I hope it helps
update code for issue : SlapperAutoMapper#79
elAndyG commentedon Nov 5, 2022
any chance of this getting updated? The PR above seems like a good fix.
update code for issue : SlapperAutoMapper#79