【问题标题】:Error Unable to update database to match the current model because there are pending changes错误无法更新数据库以匹配当前模型,因为有待处理的更改
【发布时间】:2019-11-05 15:54:55
【问题描述】:

我正在按照开发者教程安装应用程序,但打开网页时出现此错误。

我没有任何编程知识。我正在阅读与该主题相关的所有帖子,但我无法解决我的问题。我附上我所掌握的信息。

网页错误:

无法更新数据库以匹配当前模型,因为有 挂起的更改和自动迁移被禁用。要么写 待定模型更改为基于代码的迁移或启用自动 移民。将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 启用自动迁移。描述:一个未处理的 执行当前 Web 请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息和 它起源于代码。

异常详情:

System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException: 无法更新数据库以匹配当前模型,因为有 挂起的更改和自动迁移被禁用。要么写 待定模型更改为基于代码的迁移或启用自动 移民。将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 启用自动迁移。

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[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) +725
   System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +602
   System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +578
   System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +116
   System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +148
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +75
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +482
   VsomMonitoringTool.Mvc.MvcApplication.Application_Start() +69

[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) +546
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +171
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +255
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +347

[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) +552
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +122
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +737
-------------------------------------------------------------------------------------------------------

Web.Config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <section name="entityFramework"
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="VsomMonitoringToolConnectionString" connectionString="Data Source=localhost\VsomMonitoringDB;Initial Catalog=VsomMonitoringToolDB;User ID=sa;Password=M0n1t0r1ng" providerName="System.Data.SqlClient"/>
    <add name="VsomMonitoringToolConnectionString_DatabasePublish" connectionString="Data Source=localhost\VsomMonitoringDB;Initial Catalog=VsomMonitoringToolDB;User ID=sa;Password=M0n1t0r1ng" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <!--<authentication mode="None" />-->
    <compilation targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear/>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
      </providers>
    </membership>
    <roleManager enabled="true"/>
  </system.web>
  <!--<system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>-->
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <!--<dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>-->
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebMatrix.Data" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
    <contexts>
      <context type="VsomMonitoringTool.DataAccess.VsomMonitoringToolContext, VsomMonitoringTool.DataAccess">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[VsomMonitoringTool.DataAccess.VsomMonitoringToolContext, VsomMonitoringTool.DataAccess], [VsomMonitoringTool.DataAccess.Migrations.Configuration, VsomMonitoringTool.DataAccess]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          <parameters>
            <parameter value="VsomMonitoringToolConnectionString_DatabasePublish"/>
          </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>
<!--ProjectGuid: {465DCAD8-36C2-40E2-A742-9EC8BFCA8B9C}-->

【问题讨论】:

    标签: entity-framework


    【解决方案1】:

    有几种方法可以处理此问题,并且可能有多个问题导致此问题。

    但您可以尝试这种方法,首先Add-Migration 并将其命名为Empty

    当你的Migration被创建时,检查它是否包含任何代码,如果它包含任何额外或重复的代码,只需将其删除。

    应该是这样的:

    public partial class Empty : DbMigration
    {
        public override void Up()
        {
        }
    
        public override void Down()
        {
        }
    }
    

    现在就试试吧。

    【讨论】:

      【解决方案2】:

      非常感谢您的回复! 对不起我的问题,但我对这个主题一无所知。 我应该如何执行添加迁移命令? 根据我正在阅读的内容,我从 Visual Studio> Package Manager Console 进入并执行了命令。在这样做时,他告诉我以下内容:

      PM> 添加迁移。 命令管道位置 1 处的 cmdlet Add-Migration。 为以下参数提供值: 名称:空 正在运行的命令因首选项变量而停止。 “ErrorActionPreference”或常用参数设置为停止:找不到路径'C:\Users\Administrator\Source\Repos\VsomMonitoringTool\http:\localhost\VsomMonitoringTool',因为它不存在。 下午>

      enter image description here

      打开应用程序代码并执行命令的正确方法是什么?

      【讨论】:

        猜你喜欢
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 1970-01-01
        • 2011-06-23
        • 2016-07-09
        • 2012-01-07
        相关资源
        最近更新 更多