简体   繁体   English

实体框架代码优先定位LocalDb而不是本地SQL Server Express主机

[英]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.

相关问题 实体框架代码优先迁移与SQL Server失败 - Entity Framework Code-First Migration Fails with SQL Server 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship 使用代码优先迁移的Entity Framework从SQL Server到Oracle - From SQL Server to Oracle using Entity Framework with code-first Migrations SQL Server表设计建议(Entity Framework代码优先,C#) - SQL Server table design advice (Entity Framework code-first, C#) 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 实体框架代码优先连接服务器错误 - Entity framework code-first connect server error SQL Server Express的代码优先; 操作系统错误2 - Code-First with SQL Server Express ; Operating System Error 2 首先是实体框架代码的LocalDB数据库 - LocalDB database on fly for entity framework code first 实体框架6代码优先的迁移生成 - Entity Framework 6 code-first migrations generation 实体框架5代码优先配置封装 - Entity Framework 5 code-first configuration encapsulation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM