从EF自动迁移切换到代码优先迁移

Nat*_*rty 6 database asp.net entity-framework ef-migrations

我目前正在尝试从实体框架自动迁移切换到代码优先迁移.对于一个小背景故事,这个解决方案分为四个单独的项目:数据,模型,服务和Web.连接字符串信息位于Web项目中,上下文位于数据项目中.

现在,我已经运行了"启用 - 迁移",这似乎已正常运行.

从那里,我删除了数据库中现有的migrationHistory表.

运行

Add-Migration -projectName Data" 
Run Code Online (Sandbox Code Playgroud)

将生成适当的迁移.

问题是当我尝试运行Update-Database时,会导致一般错误:

> A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or
> was not accessible. Verify that the instance name is correct and that
> SQL Server is configured to allow remote connections. (provider: SQL
> Network Interfaces, error: 26 - Error Locating Server/Instance
> Specified)
Run Code Online (Sandbox Code Playgroud)

我可以使用web.config中提供的凭据连接到数据库,并且数据库连接在过去几个月中已正常工作,因此我认为这不是连接问题,除非代码优先迁移需要不同于自动的端口移民.

我假设它只是没有看到web.config中提供的连接字符串,无论如何运行

Update-Database -projectName data -startupprojectname web
Run Code Online (Sandbox Code Playgroud)

导致相同的错误

我的问题:

在给出上述信息的情况下,如何解决与网络相关的通用错误?这个问题是否是一个可见性问题,其中数据项目代码优先迁移无法在Web项目中看到连接字符串?

Nat*_*rty 1

代码优先迁移 - 它将使用什么连接字符串?

在更新数据库/添加迁移中指定连接字符串名称工作正常。

Update-Database -projectName Data -connectionStringName MyConnectionString
Run Code Online (Sandbox Code Playgroud)