2016-12-04 123 views
0

我在尝试使用代码第一次迁移来更新数据库时一直在努力寻找对以下错误的解决方案。代码优先迁移 - 更新数据库错误 - 网络相关实例?

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: 50 – Local Database Runtime error occurred. The specified LocalDB instance does not exist.

我设法找到以下内容,我想知道是否有任何其他方法来解决错误。

我的回答:


确保您的SQL Server Management Studio中被安装在PC上,你的SQL Server实例应SQLEXPRESS V11.0。为什么选择V11.0?因为用于代码的Visual Studio本地数据库首先适用于版本11.0。

转到SQL Server配置管理器,展开SQL Server网络配置并双击协议SQLEXPRESS

确保命名管道和TCP/IP已启用。

回到你的web应用程序,点击你的web.config。您的连接字符串应该是

<connectionStrings> 
    <add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-EMS_Events-20151217024735.mdf;Initial Catalog=aspnet-EMS_Events-20151217024735;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 

请确保它转到(LocalDb)\v11.0

转到包管理器控制台并运行update-database

+0

请添加标签数据库+版本 –

+0

其V11.0我不能在标签找到DB版本 –

+0

你应该张贴你的答案*为*的答案,而不是问题的一部分。 –

回答

2

我尝试使用默认选项时遇到了很多问题。我更喜欢在使用添加迁移或更新数据库时指定连接字符串。我也可以使用开发服务器而不是SQL Express。

Add-Migration AddSomeThing -ConnectionString "Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-EMS_Events-20151217024735.mdf;Initial Catalog=aspnet-EMS_Events-20151217024735;Integrated Security=True" -ConnectionProviderName "System.Data.SqlClient" -Verbose 

Update-Database -ConnectionString "Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-EMS_Events-20151217024735.mdf;Initial Catalog=aspnet-EMS_Events-20151217024735;Integrated Security=True" -ConnectionProviderName "System.Data.SqlClient" -Verbose 
相关问题