Entity Framework 根本没有应用迁移
Entity Framework Migrations not getting applied at all
谁能解释一下如何访问迁移修改的数据库?我一直在进行迁移和恢复,但我不知道哪个数据库被修改了。我打开 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=\"Web\" /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 中完成的教程。
<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
这 3 个命令应该创建数据库。
我使用了以下方法来解决这个问题。
打开包管理器控制台并执行以下命令
1>Enable-Migrations
已经启用
2>Add-Migration初始
完全的
3>Update-Database
指定 '-Verbose' 标志以查看 SQL......
解决方案 1:- 我打开了数据库并删除了 __MigrationHistory
和 运行 命令 update-database 命令
解决方案 2:如果第一个解决方案不起作用,则从数据库中删除所有 table
和管理控制台中的 运行 update-database 命令
您的结构化数据将可用。
这可能会解决您的问题。
谢谢
萨米尔·巴哈德
- 清理你的项目,
- 再次构建(不要使用重建),
- 然后 运行 在包管理器控制台更新数据库。
这 3 个步骤对我来说总是有效,不会丢失任何数据。
将您正在处理的 (DAL) 项目设为启动项目,然后重试
谁能解释一下如何访问迁移修改的数据库?我一直在进行迁移和恢复,但我不知道哪个数据库被修改了。我打开 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=\"Web\" /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 中完成的教程。
<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
这 3 个命令应该创建数据库。
我使用了以下方法来解决这个问题。 打开包管理器控制台并执行以下命令
1>Enable-Migrations 已经启用 2>Add-Migration初始 完全的 3>Update-Database 指定 '-Verbose' 标志以查看 SQL......
解决方案 1:- 我打开了数据库并删除了 __MigrationHistory 和 运行 命令 update-database 命令
解决方案 2:如果第一个解决方案不起作用,则从数据库中删除所有 table 和管理控制台中的 运行 update-database 命令 您的结构化数据将可用。
这可能会解决您的问题。
谢谢 萨米尔·巴哈德
- 清理你的项目,
- 再次构建(不要使用重建),
- 然后 运行 在包管理器控制台更新数据库。
这 3 个步骤对我来说总是有效,不会丢失任何数据。
将您正在处理的 (DAL) 项目设为启动项目,然后重试