web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

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

(0) ShareShare
ReportReport
Posted on by Moderator

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?

*This post is locked for comments

I have the same question (0)
  • Daniel Weichsel Profile Picture
    on at

    Hi Tommy,

    Although the exception is thrown where you have highlighted, could the problem be earlier in the method at the ToDictionary call due to delayed evaluation of the Linq expressions?  The stack trace seems to blame ToDictionary because of a duplicate key.

    Have you tried using AxUtilLib in a C# program to gather all the element handles for your model file and the installed models?  Passing true as the second parameter to AxUtil.View will make the returned ModelContents object include the elements collection, including handles.

  • Tommy Skaue Profile Picture
    Moderator on at

    Hi Daniel.

    I will have a look at that. Right now I am just utterly frustrated with the lack of auditing and logging of this "black box". I have tried to use SQL Profiler to find what element is causing the issue, but without the ability to step through AxUtilLib I am more or less forced to create a support ticket to Microsoft.

    I will look into the View and see if I can get something out of it. :-)

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    You can use Reflector to generate symbols and place breakpoints to almost any code - I've found it extremely useful. It's not free but you can use the trial version for your current task (and think later about whether the investment isn't worth for you or your company).

  • Tommy Skaue Profile Picture
    Moderator on at

    Thanks for the tip, Martin! I have used the Reflector tool before, while it was free. 

    So I downloaded the 14 day free trial and enabled debugging on AxUtilLib from within Visual Studio 2010. It worked like charm. I then used the Call Stack to narrow down where the error happened and step through the code. 

    I found the exact location where the program throws, and found a list of elements where two of them had identical ElementHandle Id. This causes the Linq provider to throw, since ElementHandle was the key.

    Since this was a form element, I just opened AX and deleted the entire form within the Developer Workspace. I then tried to install the model again, and voila - IT worked!

    Here are some screenshots:

    1007.point_5F00_of_5F00_error.gif

    Problem solved! 

    The developer behind this form says he don't have any exact reason for why this form bugged the installer, but he remembers copying the design from one form to another. This might have caused the issue. The form was developed on AX2012 RTM (probably CU3).

  • Brandon Ahmad Profile Picture
    2,465 User Group Leader on at

    Very, very helpful post. Maybe this will help me solve a LINQ problem that doesn't show up in LINQ to Entities, but shows up with LINQ to Dynamics with MVC.   Helpful indeed..  This is my favorite post over the last 2 months.  

  • Brandon Ahmad Profile Picture
    2,465 User Group Leader on at

    Gentlemen, I have one last question.  Do you know what the advantage of the reflector would be over WinDbg?  With WinDbg, we can download the symbols.  I'm just curious.  

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    The answer is - they do completely different things. The main feature of Reflector is decompiling CIL to C#, so you'll get code in a high-level language. The feature for generating symbols allows you to debug running programs - this is basically the same as if you generate symbols from your own code. If you can download symbols, you don't need to generate them, of course, you'll just say Visual Studio where to find them - as you do in WinDbg.

    WinDBg, on the other hand, is primarily used to analyze programs *after* they ran, therefore it allows you to analyze memory dumps and such things, but not to decompile CIL or generate symbols.

    You could use Reflector to generate symbols and use them in WinDbg, if you needed to do a post-mortem analysis of some application without symbols.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans