Entity Framework 6 update-database throws error while unloading appdomain

1

We have an EF6 application that is currently using automatic migrations. Our DbMigrationsConfiguration class is:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

This has all been working fine for a few months, somewhere in the last week it has started to throw an error whilst seeding. I have looked through all the change sets (a fair few), and can not see anything that has changed on the context object or configuration that might have effected this. There are no external DLL's being called.

The seeding will always run but at the end it will throw:

System.CannotUnloadAppDomainException: Error while unloading appdomain.(Exception from HRESULT: 0x80131015)
at System.AppDomain.Unload(AppDomain domain)
at System.Data.Entity.Migrations.Design.ToolingFacade.Dispose(Boolean disposing)
at System.Data.Entity.Migrations.Design.ToolingFacade.Dispose()
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

Error while unloading appdomain. (Exception from HRESULT: 0x80131015)

If I remove any use of the context object from the seeding it will work fine.

If I even do:

var objs = context.ObjectX.ToList();

It will throw the error.

I have tried remove all references to EF, deleting the packages and re-downloading them, deleting the database, using a new database but it will always throw the error.

I am not sure how to track down what is causing this. Any thoughts / processes / ideas on how to handle / fix / track down this would be great.

c#
entity-framework
asked on Stack Overflow Mar 12, 2015 by Jon • edited Mar 12, 2015 by VMAtm

2 Answers

2

(Opening out my comment)

Happily you can see the source at https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Migrations/Design/ToolingFacade.cs - when looking for migration classes, it loads them into its own AppDomain. When finished it uploads the appdomain.

The exception documentation, https://msdn.microsoft.com/en-us/library/system.cannotunloadappdomainexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396, says there are 3 reasons for the exception - it must be the 2nd or 3rd. Already unloaded (disposing twice?) or a running thread. No threads created, so maybe it's disposed twice?

It could also be something to do with static (or other) constructors in the migrations type doing something long-running? Or something that they depend on.

answered on Stack Overflow Mar 16, 2015 by Kieren Johnstone
0

HRESULT: 0x80131015 for such errors means that you got something wrong into your COM part of code. As you can see from stacktrace, the error is being rised during MigrationsDomainCommand.Execute.

So the problem is that your data doesn't being sent correctly to the server or MigrationsDomainCommand didn't recieve success result from database in some amount of time.

May be your application tries to migrate to much data, or something? Check the timeout for your connection AND command - they have different timeouts by default. Or may be it simply trying to use a disposed command or collection.

answered on Stack Overflow Mar 12, 2015 by VMAtm • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0