locked
Runtime exception from type name conflict: "An item with the same key has already been added" RRS feed

  • Question

  • Hey all-

    (Advance apologies - this must be a faq, but I'm not finding it....)

    There are two classes named "TreeNode" in my solution.  They are in different class libraries, and different namespaces.  One is generated by our EF 4.1 model (POCO).  The other is unrelated to EF.

    At runtime, the model loads fine and a variety of entities can be accessed (including the entities used for authentication).  But a few clicks into our web app, attempting to load one of our pages gives the exception below.

    If I refactor/rename the non-EF class, everything works ok.  What gets me is that this arrangement works fine with a dozen other conflicting names in the same two assemblies of our codebase.

    Is there some magical combination of "using" statements that will hide the non-EF class from EF's runtime detection?  There really doesn't seem to be any reason for EF to get confused by this completely unrelated class's mere existence.

    Thanks much,

    Eric

    Exception Details: System.ArgumentException: An item with the same key has already been added.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace:


    [ArgumentException: An item with the same key has already been added.]
       System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12679472
       System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +843

    [MappingException: An EdmType cannot be mapped to CLR classes multiple times. The EdmType 'ContentModel.TreeNode' is mapped more than once.]
       System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +1492
       System.Data.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly assembly, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +193
       System.Data.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action`1 logLoadMessage) +140

    Wednesday, May 16, 2012 2:25 AM

All replies

  • Also worth noting that when it's been cached, this page will give a slightly more informative MetadataException.  The top 3 lines of the callstack are the same methods, we just crash at a different line number.

    Exception Details: System.Data.MetadataException: Schema specified is not valid. Errors:
    The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'TreeNode'. Previously found CLR type 'Snip.Content.TreeNode', newly found CLR type 'Snip.Tree.TreeNode'.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace:


    [MetadataException: Schema specified is not valid. Errors:
    The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'TreeNode'. Previously found CLR type 'Snip.Content.TreeNode', newly found CLR type 'Snip.Tree.TreeNode'.]
       System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +554
       System.Data.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly assembly, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +193
       System.Data.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action`1 logLoadMessage) +140


    • Edited by EricDAllen Wednesday, May 16, 2012 2:38 AM Improvement
    Wednesday, May 16, 2012 2:37 AM
  • Hi EricDAllen,

    Welcome to MSDN Forum.

    Could you please post some code here? Or upload a demo to SkyDrive and post the link here. I will download it and try my best to help you find the solution.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Allen_MSDN Saturday, May 19, 2012 6:39 AM
    Saturday, May 19, 2012 6:20 AM