实体框架迁移根本没有得到应用

时间:2015-11-28 22:25:55

标签: asp.net entity-framework migration

有人可以解释如何访问迁移修改的数据库吗?我一直在进行迁移并恢复,但我不知道哪个数据库被修改。我打开SQL对象资源管理器并刷新并看不到任何内容,我打开服务器资源管理器并断开所有连接,并以某种方式我的迁移仍然应用。 VS2015究竟出了什么问题?这是我还是软件根本无法正常工作?

Specify the '-Verbose' flag to view the SQL statements being applied to 

the target database.
Applying explicit migrations: [201511282210132_Initial].
Applying explicit migration: 201511282210132_Initial.
Running Seed method.
PM> update-database -target:0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Reverting migrations: [201511282210132_Initial].
Reverting explicit migration: 201511282210132_Initial.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201511282210132_Initial].
Applying explicit migration: 201511282210132_Initial.
Running Seed method.
PM> update-database -target:0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Reverting migrations: [201511282210132_Initial].
Reverting explicit migration: 201511282210132_Initial.
PM> update-database -target:0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is already at version 0.
PM> 

有人告诉我,如果迁移得到应用,则意味着VS2015成功修改了数据库,但似乎根本不是这样。

我的配置文件可能有问题:

<?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=301879
-->
<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="DefaultConnection" connectionString="Server=DESKTOP;Database=API;Trusted_Connection=True;"
        providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings></appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <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.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
<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>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
</configuration>

此外,App_Data中没有文件。有人告诉我应该有一个mdf文件,但我找不到任何东西。也许VS2015出了问题。另一件需要考虑的事情是我正在尝试使用VS2015在VS2013中完成一个教程。

4 个答案:

答案 0 :(得分:2)

  <connectionStrings>
    <!-- Original Connection String -->
    <!--
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;
         Initial Catalog=aspnet-AspnetIdentitySample-20130627083537_2;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    -->
    <!-- New Connection String -->
    <add name="MyConnectionString" connectionString="Data Source=(LocalDb)\v11.0;
         Initial Catalog=MyAspnetIdentitySample_1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

好的,问题是我没有在构造函数中放置connectionString。出于某种原因,在教程中它没有它,但是从那时起ASP.NET似乎发生了很大变化,所以如果你最终遇到和我一样的问题,你应该做一个。

public class MyDbContext : DbContext{
   public MyDbContext() : base("MyConnectionString") { }
}

执行此操作后,只需回滚所有迁移,删除迁移文件夹并在包管理控制台中写入:

>Enable-Migrations
>Add-Migration Initial
>Update-Database

这三个命令应该创建数据库。

答案 1 :(得分:1)

我使用以下方法来解决问题。 打开包管理器控制台和以下命令

1&GT;使能的迁移 已启用 2&gt; Add-Migration Initial 完成 3 GT;更新的数据库的 指定&#39; -Verbose&#39;用于查看SQL的标志......

解决方案1: - 我打开了数据库并删除了 __ MigrationHistory 并运行命令update-database命令

解决方案2:如果第一个解决方案不起作用,则从数据库中删除所有表 并在管理控制台中运行update-database命令 您的结构数据将可用。

这可以解决您的问题。

由于 Sameer Bahad

答案 2 :(得分:0)

  1. 清理您的项目,
  2. 再次构建(不要使用重建),
  3. 然后在包管理器控制台中运行 update-database。

这 3 个步骤总是对我有用,不会丢失任何数据。

答案 3 :(得分:0)

将您正在处理的 (DAL) 项目设为启动项目,然后重试