3

Closed

The Ssdl we save in __MigrationHistory table is invalid

description

The Ssdl we save in the __MigrationHistory table is invalid. For some reason the error is not reported on the .NET Framework 4 when reading StoreItemCollection (either a weird xsd bug or we setup something incorrectly since the error is reported when opening the ssdl (as xml) in VS). However on Mono Key element is marked as invalid. This results in an assert following by NRE on Mono. The bug is that we write IsSystem attribute on the Key element (<Key p5:IsSystem=""true"">) while according to the schema the Key element cannot have any attributes - even in a non-empty, non-EF namespace.

Note this seems to completely block EF6 on Mono

Repro - this does not throw on .NET Framework
            var ssdl = XDocument.Parse(
@"<Schema Namespace=""CodeFirstDatabaseSchema"" Provider=""Devart.Data.MySql"" ProviderManifestToken=""MySQL"" Alias=""Self"" xmlns=""http://schemas.microsoft.com/ado/2009/11/edm/ssdl"">
  <EntityContainer Name=""CodeFirstDatabase"">
    <EntitySet Name=""HistoryRow"" EntityType=""Self.HistoryRow"" Schema=""dbo"" Table=""__MigrationHistory"" p6:IsSystem=""true"" xmlns:p6=""http://schemas.microsoft.com/ado/2012/10/edm/migrations"" />
  </EntityContainer>
  <EntityType Name=""HistoryRow"" p5:IsSystem=""true"" xmlns:p5=""http://schemas.microsoft.com/ado/2012/10/edm/migrations"">
    <Key p5:IsSystem=""true"">
      <PropertyRef Name=""MigrationId"" p5:IsSystem=""true"" />
      <PropertyRef Name=""ContextKey"" p5:IsSystem=""true"" />
    </Key>
    <Property Name=""MigrationId"" Type=""varchar"" MaxLength=""255"" Nullable=""false"" p5:IsSystem=""true"" />
    <Property Name=""ContextKey"" Type=""varchar"" MaxLength=""512"" Nullable=""false"" p5:IsSystem=""true"" />
    <Property Name=""Model"" Type=""longblob"" Nullable=""false"" p5:IsSystem=""true"" />
    <Property Name=""ProductVersion"" Type=""varchar"" MaxLength=""32"" Nullable=""false"" p5:IsSystem=""true"" />
  </EntityType>
</Schema>");

            new StoreItemCollection(new[] { ssdl.CreateReader() });
but on Mono this throws NRE:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.EntityKeyElement.Validate () [0x00000] in <filename unknown>:0
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaEntityType.Validate () [0x00000] in <filename unknown>:0
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.Validate () [0x00000] in <filename unknown>:0
  at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.ValidateSchema () [0x00000] in <filename unknown>:0
  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) [0x00000] in <filename unknown>:0
...
The fix is to allow xs:AnyAttribet on the TEntityKeyElement
    <xs:complexType name="TEntityKeyElement">
        <xs:sequence>
            <xs:element name="PropertyRef" type="edm:TPropertyRef" minOccurs="1" maxOccurs="unbounded" />
            <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />          
        </xs:sequence>
        <!-- add this -->
        <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
Related issue: https://entityframework.codeplex.com/workitem/1038
Closed Oct 1, 2013 at 6:53 PM by RoMiller
EF Team Triage: We are no longer seeing these issues.

comments

Geminiman wrote May 10, 2013 at 1:35 PM

FYI, this also happens on .NET 4.5 on Windows with the latest nightlies.

If you put in a static constructor on your DbContext in code first that has this in it:

System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DBContextName, Migrations.Configuration>());

It will fail while running that every time.

janoveh wrote May 18, 2013 at 2:44 PM

I'm seeing the same problem. In fact, I'm stuck... I cannot migrate my model any longer.
I'm now on "6.0.0-beta1-20517" and using SqlCe. I hope you can fix this soon... Please...
Here is a dump from the package manager console:

PM> add-migration Evo10 -Force
System.Data.Entity.Core.MappingException: Schema specified is not valid. Errors:
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.Init(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable1 xmlReaders, IList1 filePaths, Boolean throwOnError)
at System.Data.Entity.Core.Mapping.StorageMappingItemCollection..ctor(EdmItemCollection edmCollection, StoreItemCollection storeCollection, IEnumerable`1 xmlReaders)
at System.Data.Entity.Utilities.XDocumentExtensions.GetStorageMappingItemCollection(XDocument model, DbProviderInfo& providerInfo)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Schema specified is not valid. Errors:
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
<File Unknown>(0,0) : error 2025: XML Schema validation failed for mapping schema. Schema Error Information : The 'http://schemas.microsoft.com/ado/2012/10/edm/migrations:IsSystem' attribute is not declared..
PM>

moozzyk wrote May 18, 2013 at 5:15 PM

@janoveh: I believe you are actually seeing this: http://entityframework.codeplex.com/workitem/1128. It's a bit different issue even though on the surface looks the same.

janoveh wrote May 19, 2013 at 10:39 AM

@moozzyk Thank You! You saved my day!

IDisposable wrote Jun 7, 2013 at 10:34 PM

Where do I make the attribute change to enable migrations to load?

moozzyk wrote Jun 8, 2013 at 12:45 AM

This would be in the System.Data.Resources.SSDLSchema_3.xsd. Relaxing this however would mean that a v3 SSDL that can be parsed with EF6 will fail on EF5. I don't know how important it is since we have never heard anyone complaining that custom attributes are not allowed on entity key elements. Also, note that in EF6 beta1 we no longer create the invalid SSDL