Quantcast
Channel: Microsoft Dynamics AX Forum - Recent Threads
Viewing all articles
Browse latest Browse all 73760

An item with the same key has already been added - Import of Model

$
0
0

During import of a new updated version of a model, I am getting the error "An item with the same key has already been added".

Since the error is very generic, and does not really say what element is cause the problem, I made a quick C# Console project. The call stack reveals the following :

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelContents.UpdatePaths()
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelStore.GetOrphansInclusiveParentPreDelete(SqlCommand command)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelStore.GetOrphansPreDeletePublisherCategory(SqlConnection connection, SqlTransaction transaction, CategoryType category, String publisher)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelImport.ModelImportReader.BuildOrphanListAndPrompt(SqlConnection connection, SqlTransaction transaction, Dictionary`2 tempIdToModelId, String publisher)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelImport.ModelImportReader.ResolveReplace(SqlConnection connection, SqlTransaction transaction)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelImport.ModelImportReader.ConflictResolve(SqlConnection connection, SqlTransaction transaction)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelImport.ModelImportReader.Import()
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.ModelImport.ModelImportReader.Import()
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.AxUtil.Import(Boolean verbose, Nullable`1 conflictTargetLayer)
at Microsoft.Dynamics.AX.Framework.Tools.ModelManagement.AxUtil.Import(AxUtilContext utilContext, AxUtilConfiguration utilConfig, Boolean verbose)
at ModelImportTest4.Program.Main(String[] args) in C:\Users\skautomm\documents\visual studio 2010\Projects\ModelImportTest4\ModelImportTest4\Program.cs:line 25
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I reverse engineered the AxUtilLib and found the following body in the method throwing the error:

internal void UpdatePaths()
{
Dictionary<int, ModelElementInfo> dictionary = Enumerable.ToDictionary<ModelElementInfo, int>((IEnumerable<ModelElementInfo>) Enumerable.OrderBy<ModelElementInfo, int>((IEnumerable<ModelElementInfo>) this.Elements, (Func<ModelElementInfo, int>) (elmKey => elmKey.ElementHandle)), (Func<ModelElementInfo, int>) (keySelector => keySelector.ElementHandle));
foreach (ModelElementInfo modelElementInfo1 in dictionary.Values)
{
ModelElementInfo modelElementInfo2;
dictionary.TryGetValue(modelElementInfo1.ParentHandle, out modelElementInfo2);
if (modelElementInfo2 != null)
{
switch (modelElementInfo1.ElementType)
{
case ElementType.ClassHeader:
case ElementType.TableHeader:
modelElementInfo1.Path = ElementTypePath.GetListName(modelElementInfo2.ElementType) + "\\" + modelElementInfo2.Name + "\\Header";
continue;
case ElementType.VisualStudioProjectFolder:
case ElementType.VisualStudioProjectFile:
modelElementInfo1.Path = modelElementInfo2.Path + "\\" + modelElementInfo1.Name;
continue;
default:
string listName = ElementTypePath.GetListName(modelElementInfo1.ElementType);
if (!string.IsNullOrEmpty(listName))
{
modelElementInfo1.Path = modelElementInfo2.Path + "\\" + listName + "\\" + modelElementInfo1.Name;
continue;
}
else
{
modelElementInfo1.Path = modelElementInfo2.Path + "\\" + modelElementInfo1.Name;
continue;
}
}
}
else
modelElementInfo1.Path = ElementTypePath.GetListName(modelElementInfo1.ElementType) + "\\" + modelElementInfo1.Name;
}
this.elements = (IList<ModelElementInfo>) Enumerable.ToList<ModelElementInfo>((IEnumerable<ModelElementInfo>) Enumerable.OrderBy<ModelElementInfo, string>((IEnumerable<ModelElementInfo>) this.elements, (Func<ModelElementInfo, string>) (e => e.Path)));
}
}

That Linq-expression is throwing.

Since Microsoft has not been kind enough to let us have the Symbols for debugging, I am sort of searching for the needle in the haystack here. This model contains nearly three thousand elements. Using elimination method is tedious and time consuming. Obviously, I am dealing with an obsolete element handle most likely being modified in a higher layer but removed in the model I am importing.

Any bright ideas on how I would go around to find the duplicate element?


Viewing all articles
Browse latest Browse all 73760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>