Bug 10245 - Duplicate key error on Entity Framework metadata loading
Summary: Duplicate key error on Entity Framework metadata loading
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-02-12 13:23 UTC by Oleg
Modified: 2013-02-13 07:15 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample project to reproduce error (989.36 KB, application/zip)
2013-02-12 13:23 UTC, Oleg
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Oleg 2013-02-12 13:23:00 UTC
Created attachment 3360 [details]
Sample project to reproduce error

I assembled simple application to test Entity Framework after bug 6653 was fixed. That problem with stack overflow gone, but it looks like there is still no way to use EF with model first approach. Sample application fault on the stage of metadata load with error "An element with the same key already exists in the dictionary".

Sample MonoDevelop project attached.

Full exception:

System.ArgumentException: An element with the same key already exists in the dictionary.
  at System.Collections.Generic.Dictionary`2[System.String,System.Object].Add (System.String key, System.Object value) [0x0007e] in /usr/ports/lang/mono/work/mono-3.0.3/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:423
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.TypeUsageBuilder.HandleNullableAttribute (System.Xml.XmlReader reader) [0x00015] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/TypeUsageBuilder.cs:605
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.TypeUsageBuilder.InternalHandleAttribute (System.Xml.XmlReader reader) [0x00010] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/TypeUsageBuilder.cs:300
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.TypeUsageBuilder.HandleAttribute (System.Xml.XmlReader reader) [0x00000] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/TypeUsageBuilder.cs:291
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.StructuredProperty.HandleAttribute (System.Xml.XmlReader reader) [0x00040] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/StructuredProperty.cs:227
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.ParseAttribute (System.Xml.XmlReader reader) [0x0006a] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:592
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.Parse (System.Xml.XmlReader reader) [0x0001d] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:193
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.StructuredType.HandlePropertyElement (System.Xml.XmlReader reader) [0x00007] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/StructuredType.cs:387
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.StructuredType.HandleElement (System.Xml.XmlReader reader) [0x0001f] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/StructuredType.cs:267
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaEntityType.HandleElement (System.Xml.XmlReader reader) [0x00000] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/ItemType.cs:198
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.ParseElement (System.Xml.XmlReader reader) [0x0002c] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:746
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.Parse (System.Xml.XmlReader reader) [0x000d0] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:220
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.HandleEntityTypeElement (System.Xml.XmlReader reader) [0x00007] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/Schema.cs:1023
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.HandleElement (System.Xml.XmlReader reader) [0x0001f] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/Schema.cs:592
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.ParseElement (System.Xml.XmlReader reader) [0x0002c] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:746
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaElement.Parse (System.Xml.XmlReader reader) [0x000d0] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaElement.cs:220
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.HandleTopLevelSchemaElement (System.Xml.XmlReader reader) [0x0000e] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/Schema.cs:1005
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.InternalParse (System.Xml.XmlReader sourceReader, System.String sourceLocation) [0x0021e] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/Schema.cs:210
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.Parse (System.Xml.XmlReader sourceReader, System.String sourceLocation) [0x0000f] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/Schema.cs:100
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaManager.ParseAndValidate (IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, System.Data.Entity.Core.EntityModel.SchemaObjectModel.AttributeValueNotification providerNotification, System.Data.Entity.Core.EntityModel.SchemaObjectModel.AttributeValueNotification providerManifestTokenNotification, System.Data.Entity.Core.EntityModel.SchemaObjectModel.ProviderManifestNeeded providerManifestNeeded, IList`1& schemaCollection) [0x0008a] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaManager.cs:152
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaManager.ParseAndValidate (IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, System.Data.Entity.Core.Common.DbProviderManifest providerManifest, IList`1& schemaCollection) [0x0000d] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityModel/SchemaObjectModel/SchemaManager.cs:102
  at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.LoadItems (IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModelOption, System.Data.Entity.Core.Common.DbProviderManifest providerManifest, System.Data.Entity.Core.Metadata.Edm.ItemCollection itemCollection, Boolean throwOnError) [0x00002] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/EdmItemCollection.cs:245
  at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.Init (IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError) [0x00006] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/EdmItemCollection.cs:126
  at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection..ctor (IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean skipInitialization) [0x00023] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/EdmItemCollection.cs:41
  at System.Data.Entity.Core.Metadata.Edm.MetadataCache+EdmMetadataEntry.LoadEdmItemCollection (System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader loader) [0x00008] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/MetadataCache.cs:653
  at System.Data.Entity.Core.Metadata.Edm.MetadataCache+EdmItemCollectionLoader.LoadItemCollection (System.Data.Entity.Core.Metadata.Edm.EdmMetadataEntry entry) [0x00000] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/MetadataCache.cs:834
  at System.Data.Entity.Core.Metadata.Edm.MetadataCache.LoadItemCollection[EdmMetadataEntry] (IItemCollectionLoader`1 itemCollectionLoader, System.Data.Entity.Core.Metadata.Edm.EdmMetadataEntry entry) [0x00037] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/MetadataCache.cs:367
  at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetOrCreateEdmItemCollection (System.String cacheKey, System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader loader, System.Object& entryToken) [0x00020] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Metadata/MetadataCache.cs:273
  at System.Data.Entity.Core.EntityClient.EntityConnection.LoadEdmItemCollection (System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace workspace, System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader artifactLoader) [0x0000f] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityClient/EntityConnection.cs:1053
  at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace (Boolean initializeAllCollections) [0x000b6] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/EntityClient/EntityConnection.cs:467
  at System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection () [0x00017] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Objects/ObjectContext.cs:1839
  at System.Data.Entity.Core.Objects.ObjectContext..ctor (System.Data.Entity.Core.EntityClient.EntityConnection connection, Boolean isConnectionConstructor, System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, System.Data.Entity.Core.Common.Internal.Materialization.Translator translator) [0x000bc] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Objects/ObjectContext.cs:192
  at System.Data.Entity.Core.Objects.ObjectContext..ctor (System.String connectionString) [0x00000] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Objects/ObjectContext.cs:114
  at System.Data.Entity.Core.Objects.ObjectContext..ctor (System.String connectionString, System.String defaultContainerName) [0x00000] in /usr/ports/lang/mono/work/mono-3.0.3/external/entityframework/src/EntityFramework/Core/Objects/ObjectContext.cs:134
  at TestModel.TestEntities..ctor () [0x00000] in /home/Test/Test/TestModel.Designer.cs:46
  at Test.Program.Main (System.String[] args) [0x00001] in /home/Test/Test/Program.cs:11
Comment 1 Atsushi Eno 2013-02-13 03:46:29 UTC
I think I found the cause of the problem:

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Test
{
	public static void Main ()
	{
		string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo'>
  <xs:element name='root'>
    <xs:complexType>
      <xs:attribute name='d' default='v' use='optional' />
    </xs:complexType>
  </xs:element>
</xs:schema>";
		string xml = "<root xmlns='urn:foo' />";
		var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
		xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
		var xr = XmlReader.Create (new StringReader (xml), xrs);
		xr.Read ();
		Console.WriteLine (xr.LocalName);
		bool more;
		for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ())
			Console.WriteLine (xr.LocalName);
		xr.MoveToElement ();
		Console.WriteLine (xr.AttributeCount);
		for (int i = 0; i < xr.AttributeCount; i++) {
			xr.MoveToAttribute (i);
			Console.WriteLine (xr.LocalName);
		}
	}
}
Comment 2 Atsushi Eno 2013-02-13 07:15:43 UTC
Fixed in master (19eac76), regression due to https://github.com/mono/mono/commit/7d170f400d288f240cf231f481bad67a9e352b0c#L8L277