简体   繁体   English

无法将实体框架连接到本地SQL Server Express

[英]Can't connect Entity Framework to local SQL Server Express

I have the connection string: 我有连接字符串:

  <connectionStrings>
    <add name="SolrLocal" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLExpress;Initial Catalog=SpeedOfLightRailDB;Integrated Security=true" />
  </connectionStrings>

But I'm getting the error: 但是我得到了错误:

"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)" “建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。找不到服务器或无法访问该服务器。请验证实例名称正确并且已将SQL Server配置为允许远程连接。(提供者: SQL网络接口,错误:26-错误定位服务器/实例指定)

This is my first time playing with SQL Server Express so I might be missing something obvious.... 这是我第一次使用SQL Server Express,所以我可能会遗漏一些明显的东西。

Can anyone at least confirm that this is what the connection string should look like for Entity Framework -> SQL Sever Express? 至少有人能确认这是实体框架-> SQL Sever Express的连接字符串的样子吗? I've been changing it around trying to get it to work. 我一直在努力尝试使之工作。

  1. Check your windows services and make sure that the sql server express windows service is running. 检查Windows服务,并确保SQL Server Express Windows服务正在运行。 You can get to services by running services.msc from the RUN command bar (windows key + R). 您可以通过从RUN命令栏(Windows键+ R)运行services.msc来获得服务。 If it is not running then start it. 如果它没有运行,请启动它。
  2. Do you have Sql Server Management Tools installed? 您是否安装了Sql Server管理工具? If so you can try to connect using those tools. 如果是这样,您可以尝试使用这些工具进行连接。 If you cant connect to your sql server express instance from there, most likely you will not be able to connect from code either. 如果您无法从此处连接到sql server express实例,则很可能您也将无法通过代码进行连接。 If you do not have this installed no worries, you can still test it with the next step. 如果您没有为此安装任何后顾之忧,仍然可以进行下一步测试。
  3. If you need help building the connection string this next one is a great way to do it. 如果您需要构建连接字符串的帮助,那么下一个是一个很好的方法。 Create a text file on your desktop. 在桌面上创建一个文本文件。 Rename the extension from .txt to .udl . 将扩展名从.txt重命名为.udl Open the file and you will get a Data Link Properties screen, start with the 1st tab (it defaults to tab 2) and then fill in the second one also. 打开文件,您将看到“ 数据链接属性”屏幕,从第一个选项卡(默认为选项卡2)开始,然后也填写第二个选项卡。 Once you test your connection here and it works you can open the file using NotePad or rename the extension back to .txt and open it in NotePad . 一旦您在此处测试了连接并可以正常使用,就可以使用NotePad打开文件,或将扩展名重命名为.txt ,然后在NotePad打开文件。 The connection string will be on the 2nd line in the text file. 连接字符串将在文本文件的第二行。 Just copy and paste it to your .config file. 只需将其复制并粘贴到您的.config文件即可。

If none of this works then report back with what you have found and any errors you are getting. 如果以上方法均无效,请报告发现的内容和遇到的任何错误。

Try this: 尝试这个:

<connectionStrings>
    <add name="SolrLocal"
        providerName="System.Data.SqlClient"
        connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SpeedOfLightRailDB;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>

Also check if .\\SQLEXPRESS is accessible from SQL Server Management Studio, and if SQL Server is configured to allow remote connections in SQL Server Configuration Manager. 还检查是否可以从SQL Server Management Studio访问.\\SQLEXPRESS ,以及是否将SQL Server配置为允许SQL Server Configuration Manager中的远程连接。

在此处输入图片说明

It can be launched from cmd like this: C:\\Windows\\SysWOW64\\mmc.exe /32 C:\\Windows\\SysWOW64\\SQLServerManager12.msc 可以像这样从cmd启动它: C:\\Windows\\SysWOW64\\mmc.exe /32 C:\\Windows\\SysWOW64\\SQLServerManager12.msc

You are using .\\SQLExpress and I think your local servername is different. 您正在使用.\\SQLExpress ,我认为您的本地服务器名称不同。 What you can do is open your SQL Server and then while connecting see the server name used. 您可以做的是打开SQL Server,然后在连接时查看使用的服务器名称。 You must enter the same name in your web config. 您必须在网络配置中输入相同的名称。 Refer the below image which shows the server name while connecting to the SQL server, In my case I need to replace .\\SQLExpress with RAJSHEKAR-PC\\SQLEXPRESS 请参考下图,该图显示了连接到SQL Server时的服务器名称,对于我来说,我需要将.\\SQLExpress替换为RAJSHEKAR-PC\\SQLEXPRESS

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM