[英]Entity Framework code-first targeting LocalDb instead of local SQL Server Express host
In a new development project using Entity Framework 6.0 code-first I had to update my models quite a bit. 在一个使用Entity Framework 6.0代码的新开发项目中,我必须相当多地更新模型。 The update script that was automatically generated using
Add-Migration
in Package Manager Console got quite complex so I decided to start over. 使用Package Manager控制台中的
Add-Migration
自动生成的更新脚本非常复杂,因此我决定重新开始。 To do this I deleted the database from localhost and deleted the Migrations
folder in the project. 为此,我从本地主机中删除了数据库,并删除了项目中的“
Migrations
文件夹。 However when I re-ran the Add-Migration
-command I got the following message and the same update-script was generated again: 但是,当我重新运行
Add-Migration
命令时,收到以下消息,并再次生成相同的更新脚本:
The Designer Code for this migration file includes a snapshot of your current Code First model.
此迁移文件的Designer代码包括您当前的Code First模型的快照。 This snapshot is used to calculate the changes to your model when you scaffold the next migration.
当支持下一次迁移时,此快照用于计算对模型的更改。
I was fairly certain this was because __MigrationHistory
-table was still there somehow. 我相当确定这是因为
__MigrationHistory
仍然存在。 I went into the database again but nothing had been created and the database was still gone. 我再次进入数据库,但是什么也没有创建,数据库仍然不见了。
Connection string used in web.config
: 在
web.config
使用的连接字符串:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=localhost;Initial Catalog=WebApp;Integrated Security=sspi;Pooling=false;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I then checked my processes and saw two versions of the sqlservr.exe
-process and LocalDb came to mind. 然后,我检查了我的进程,然后想到了两个版本的
sqlservr.exe
-process和LocalDb。 I connected to (LocalDb)\\MSSQLLocalDB
and there was a new database named 'DefaultConnection' with all tables for the project. 我连接到
(LocalDb)\\MSSQLLocalDB
并且有一个名为“ DefaultConnection”的新数据库,其中包含该项目的所有表。 When I deleted this database I got a fresh script from the Add-Migration
-command. 当我删除该数据库时,我从
Add-Migration
-command中获得了一个新鲜的脚本。 Somewhere along the line a switch must have been made since I'm 100% certain I have been working against the SQL Server Express host at some time (All tables prior to the update script where there when I deleted the database) and for all my other project localhost works with Data Source=localhost
. 自从我100%确定我有时已经对SQL Server Express主机进行了工作(在更新脚本之前的所有表都删除了数据库之后)之后,对于我所有的情况,都必须进行切换其他项目localhost与
Data Source=localhost
。 I can't understand why the database name is DefaultConnection
instead of WebApp
either. 我不明白为什么数据库名称也是
DefaultConnection
而不是WebApp
。 Connecting to localhost, the database name was WebApp
. 连接到localhost时,数据库名称为
WebApp
。 Has anyone experienced this before? 有谁之前经历过这个吗?
Finally found an answer to it. 终于找到了答案。 In Package Manager Console it is not enough to set
Default project
to the project containing DbContext
you ALSO need to make sure that your website is set as the Default StartUp Project! 在Package Manager控制台中,仅将
Default project
设置为包含DbContext
的项目还不够,您还需要确保将您的网站设置为Default StartUp Project!
This small change made everything work normal again. 这个小小的变化使一切恢复正常。
I cannot find this documented anywhere so I hope it will help someone. 我在任何地方都找不到此文档,因此希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.