首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法更新数据库以匹配当前模型

无法更新数据库以匹配当前模型
EN

Stack Overflow用户
提问于 2015-06-17 09:39:07
回答 1查看 6.9K关注 0票数 0

我创建了一个全新的Azure移动服务项目,并在Azure上发布了一个新创建的服务。然后我将项目发布到Azure,并验证我可以从它发布/获取。

接下来,我通过执行guide i found online onlinefound on MSDN来设置数据迁移,从而通过nuget管理器控制台启用迁移。我完成了这些步骤,添加了我的初始迁移,并将其重新发布到Azure。我没有对我的模型做任何修改。在第一次发布后,我所做的唯一有效的事情就是启用迁移,添加一个迁移点,然后重新发布。

现在登录页面加载失败,我收到以下消息:

无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移。将挂起的模型更改写入基于代码的迁移,或者启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。

AutomaticMigrationsDisabledException:无法更新数据库以匹配当前模型,因为存在挂起的更改并且禁用了自动迁移。将挂起的模型更改写入基于代码的迁移,或者启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String,LifestreamApi.WebApiApplication.Application_Start() pendingMigrations,String targetMigrationId,String lastMigrationId) +579String targetMigration) +446String +13 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action LifestreamApi.WebApiApplication.Application_Start())+422String targetMigration) +78 LifestreamApi.WebApiConfig.Register() +158String +10

HttpException (0x80004005):无法更新数据库以匹配当前模型,因为存在挂起的更改并且禁用了自动迁移。将挂起的模型更改写入基于代码的迁移,或者启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext上下文,应用上下文) +9916673 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr HttpApplication,appContext上下文,HttpContext处理程序)+118APP状态,MethodInfo[]处理程序,MethodInfo[]处理程序,IntPtr appContext,HttpContext上下文)+172app appContext,HttpContext上下文)+336app appContext) +296

HttpException (0x80004005):无法更新数据库以匹配当前模型,因为存在挂起的更改并且禁用了自动迁移。将挂起的模型更改写入基于代码的迁移,或者启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。System.Web.HttpRuntime.FirstRequestInit(HttpContext上下文) +9930568 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext上下文)+9930568 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext上下文) +254

我已经看到了大量关于这个异常由于缺少依赖而发生的帖子,such as Json。我不知道如何解决这个问题。我正在使用Visual Studio为Azure移动服务创建的基本模板。模型本身没有任何Json属性。我也不确定如何判断哪些程序集当前驻留在服务器上。我看不到任何实际连接和管理这些文件的方法(我假设我不能,除非将其托管在VM上)。

这个数据库是在我设置移动服务时由Azure创建的,我可以使用"Try it Out“页面从其中插入和删除数据。我搞不懂为什么它会被困在这里,而实际上什么都没有改变。我只是在做基线迁移。

虽然这都是默认的,除了我是如何进行迁移的(遵循上面链接的MSDN文章),我提供了我的WebApiConfig供您查看。

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);
    }
}

因为我看到发生这种情况的许多原因都是由于缺少json程序集,所以我仔细检查了我的packages.config和web.config,并验证了包是否在其中定义。

<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>
.......

已更新

下面是我的配置类

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" }
        //    );
        //
    }
}

更新2

启用自动迁移后,我现在在运行时收到此错误。

不支持影响迁移历史系统表位置的

自动迁移(如默认架构更改)。对于影响迁移历史系统表位置的操作,请使用基于代码的迁移。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30880956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档