I have an Azure MVC Web app and a corresponding WebJob using Code First / Entity Framework 6. My database is first created locally (localdb) during the add-migration/update-database process. Everything gets created just fine, the seed method populates a bunch of preset data and the app runs fine. I then publish the Web App to Azure from VS 2015 and manually deploy the DB to Azure using the "Deploy Database to Windows Azure Database" option within SQL Management Studio. The DB deploys and the Web App runs on Azure without any issues.
The problem appears when the WebJob is triggered (via a QueueTrigger). The WebJob is also using EF and it's own instance of my DbContext (due to WebJob parallelism and EF not being thread safe, I have a using statement inside my WebJOb method to instantiate the DbContext on each call). Note, my DbContext is setup with a "null" initializer ("Database.SetInitializer(null);"). This I thought prevent the exact problem I am experiencing.
When the WebJob starts, it seems to try to run migrations. The first error I got was System.Data.SqlClient.SqlException: Database '<MyDbName>' already exists. Choose a different database name.
Which implied it was trying to do a DB create when it should not have.
I then tried deleting the contents of the __MigrationHistory table to try to stop any migration trigger, but go this error: System.NotSupportedException: Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
No matter what I try, the WebJob will not succeed and it looks like it's due to not honoring my null DbInitializer, but I am not sure. The web app side never has the problem.
It's also worth noting that the problem does not exist when running locally. If I start the WebJob in debug mode under VS, it runs perfectly and accesses the localdb version of my database with problem. The problem only exists in "live" Azure.
Update:
When looking at the detailed error log from Azure for the WebJob, it seems pretty clear that the job is triggering a migration attempt despite my best efforts:
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act)
at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection)
at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState)
at System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
at System.Data.Entity.DropCreateDatabaseIfModelChanges1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)
at System.Data.Entity.DbSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)
at System.Data.Entity.DbSet1.FindAsync(Object[] keyValues)
at Repository.Pattern.Ef6.Repository1.<FindAsync>d__19.MoveNext()
The calls to InitializeDatabase and DropCreateDatabaseIfModelChanges1, seem clear that my null SetInitilizer is not having the intended effect when running on Azure. I'm stumped.