Unable to update database to match the current mod

2019-09-12 12:37发布

问题:

I created a fresh Azure Mobile Services project and published to a newly created service on Azure. I then published the project to Azure and verified that I could post/get from it.

Next, I enabled migration via the nuget manager console by following a guide i found online online and that I found on MSDN to set up data migration. I walked through the steps, adding my initial migration and re-publishing it to Azure. I did not make any changes to my models. The only thing I did after the first publish, that worked, was enable migration, add a migration point and then re-publish.

Now the landing page fails to load and I am given the following message:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

[AutomaticMigrationsDisabledException: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.] System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +579 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +446 System.Data.Entity.Migrations.<>c__DisplayClassc.b__b() +13 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422 System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78 LifestreamApi.WebApiConfig.Register() +158 LifestreamApi.WebApiApplication.Application_Start() +10

[HttpException (0x80004005): Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9916673 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

I've seen a large number of posts regarding this exception happening due to missing dependencies, such as Json. I'm not sure how to solve this issue. I'm using the basic template that Visual Studio creates for Azure Mobile Services. The models themselves do not have any Json attributes on them. I'm also not sure how to tell what assemblies currently reside on the server. I do not see any way to actually connect and manage those files (I assume I can't, not without hosting this on a VM.).

The database was created by Azure when I set up the Mobile Service and I was able to insert and delete data from it using the "Try it Out" page. I'm confused why it's getting stuck here when nothing has actually changed. I'm just doing a baseline migration.

While this is all the default, with the exception of how I am doing my migration (following the MSDN article linked above), I am providing my WebApiConfig for you to see.

public static class WebApiConfig
{
    public static void Register()
    {
        AppServiceExtensionConfig.Initialize();

        // Use this class to set configuration options for your mobile service
        ConfigOptions options = new ConfigOptions();

        // Use this class to set WebAPI configuration options
        HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

        // To display errors in the browser during development, uncomment the following
        // line. Comment it out again when you deploy your service for production use.
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

        //Database.SetInitializer(new MobileServiceInitializer());
        var migrator = new DbMigrator(new Configuration());
        migrator.Update();
    }
}

public class MobileServiceInitializer : DropCreateDatabaseIfModelChanges<MobileServiceContext>
{
    public override void InitializeDatabase(MobileServiceContext context)
    {
        base.InitializeDatabase(context);
    }
}

Since a lot of the reasons I've seen this happening have been around missing json assemblies, I double checked my packages.config and my web.config and verified the package is defined in there.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:bcl="urn:schemas-microsoft-com:bcl">
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
      <bindingRedirect oldVersion="6.0.6.0" newVersion="6.0.6.0" />
    </dependentAssembly>
.......

Updated

The following is my Configuration class

using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<LifestreamApi.Models.MobileServiceContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(LifestreamApi.Models.MobileServiceContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

Update 2

After enabling automatic migrations, I now receive this error at runtime.

Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported. Please use code-based migrations for operations that affect the location of the migrations history system table.

回答1:

It happened also on my case. The whole project was working on other PC. When I tried to copy and run on a new PC, I got the same error. What I did, remove all unused references AND checked the Web.config file matched the Initial Catalog on the ConnectionString:

RemoveUnusedReferences