Entity Framework Code-First: Generate SQL script with 'Update-Database' produces XML error while parsing

17,321

I normally use a command like:

Update-Database -Script 
       -SourceMigration:"201502201618119_Migrations17"   
       -TargetMigration:"201503031134340_Migrations18"

and then run the resultant script on the server. SourceMigration is the last entry in the __MigrationHistory table on the server.

The other 'trick' I use with migration problems is to delete the migration scripts that have not been applied to the server and then run the Add-migration again. It may be that there is a conflict between two of the migrations that is causing this. Do this rather then tweaking the individual scripts.

UPDATE: The timestamp in the above is not required. For example: SourceMigration:"Migrations17" is all that is required.

Share:
17,321
domin
Author by

domin

Updated on June 04, 2022

Comments

  • domin
    domin almost 2 years

    Our project uses Entity Framework 6.0 with .NET 4.5, FAT-Client with Code-First approach.

    We have about 20 migration-files (C# partial classes) generated automatically via the cmdlet Add-Migration in the Visual Studio Package Manager Console, and applied successfully via Update-Database.

    Now, our client has an integration database that has around 10 migrations applied already, and we need to apply the remaining 10 migrations now. We used Update-Database -Script -SourceMigration:<migration10> in order to produce a SQL script for the remaining migrations. In this case – and also when using SourceMigration:$InitialDatabase – the following error is displayed:

    [...]
    Applying explicit migration: 201609141617112_HostAufbauIdentifier.
    Applying explicit migration: 201609141622583_RemPerStaWe.
    System.Xml.XmlException: 'SoftwareAuftrag_Auftrag' is an unexpected token. Expecting white space. Line 1943, position 92.
       at System.Xml.XmlTextReaderImpl.Throw(Exception e)
       at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
       at System.Xml.XmlTextReaderImpl.ThrowExpectingWhitespace(Int32 pos)
       at System.Xml.XmlTextReaderImpl.ParseAttributes()
       at System.Xml.XmlTextReaderImpl.ParseElement()
       at System.Xml.XmlTextReaderImpl.ParseElementContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
       at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
       at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
       at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
       at System.Data.Entity.Migrations.Edm.ModelCompressor.Decompress(Byte[] bytes)
       at System.Data.Entity.Migrations.DbMigration.GetModel(Func`2 modelAccessor)
       at System.Data.Entity.Migrations.DbMigration.GetTargetModel()
       at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
       at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
       at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration migration, DbMigration lastMigration)
       at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
       at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
       at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
       at System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator.ScriptUpdate(String sourceMigration, String targetMigration)
       at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdateRunner.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.ScriptUpdate(String sourceMigration, String targetMigration, Boolean force)
       at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
       at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    

    In the file that causes trouble, here 201609141622583_RemPerStaWe, absolutely nothing is done with anything that has a name of SoftwareAuftrag or alike. This stuff is done in previous migrations, and there, I also don't see any problem at all.

    I tried attaching the debugger via code, but I don't know where to set a break point. The option to 'break when an exception occurs' doesn't seem to trigger a break in this case. Maybe another sub-process is started from the package manager.

    I have no clue how I could break down the problem, and we lose a ton of time on this. Hopefully someone has a hint on this. ;)

    UPDATE

    I systematically deleted individual migration files until I found exactly 3 that in combination cause the error. But when I uncomment everything in up and down the error persists. When deleting the whole 3 migration files, the error is gone. How does that make sense? Problems with resource files? I don't know...