实体框架“底层提供程序在打开时失败” - 卡西尼号与 IIS

发布于 2024-11-04 09:09:30 字数 1473 浏览 1 评论 0原文

我有一项使用 EF 从 SQL 数据库检索数据的服务。

EF 模型位于类库中。在类库中,连接配置为:

      <add name="APIC2CEntities"
     connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYSERVER;initial catalog=MYDB;user id=MYUSER;password=THEPASSWORD;multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

该类库由 WCF 服务项目引用。在 webconfig 中,我通过以下方式控制 EF 连接:

      <add name="APIC2CEntities"
     connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYSERVER;initial catalog=MYDB;user id=MYUSER;password=THEPASSWORD;multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

当我的服务在 Cassini 中运行时,可以正确检索数据。当服务在 IIS (Windows XP) 下运行时,连接失败并出现以下异常:

底层提供商打开失败

,内部异常告诉我:

{“与 SQL Server 建立连接时发生网络相关或特定于实例的错误。找不到服务器或无法访问服务器。请验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:Named Pipes Provider,错误:40 - 无法打开到 SQL Server 的连接)"}

我了解 Cassini 正在我的登录帐户下运行,并且 IIS 正在 IUSR_MYMACHINE 帐户下运行,但是我的联系不受信任,所以这不应该很重要...

IIS 和 Cassini 之间如何以不同的方式操纵或控制 EF 连接?

谢谢,
标记

I have a service that uses EF to retrieve data from a SQL database.

The EF model is in a class library. In the class library the connection is configured as:

      <add name="APIC2CEntities"
     connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MYSERVER;initial catalog=MYDB;user id=MYUSER;password=THEPASSWORD;multipleactiveresultsets=True;App=EntityFramework""
     providerName="System.Data.EntityClient" />

This class library is referenced by a WCF service project. In webconfig I control the EF connection with:

      <add name="APIC2CEntities"
     connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MYSERVER;initial catalog=MYDB;user id=MYUSER;password=THEPASSWORD;multipleactiveresultsets=True;App=EntityFramework""
     providerName="System.Data.EntityClient" />

When my service is running in Cassini the data is retrieved correctly. When the service is running under IIS (Windows XP) the connection fails with the following exception:

The underlying provider failed on Open

with an inner exception telling me:

{"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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}

I understand that Cassini is running under my logged in account, and that IIS is running under the IUSR_MYMACHINE account, but my connection is not trusted, so this shouldn't matter...

How else might an EF connection be manipulated or controlled differently between IIS and Cassini?

Thanks,
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风起 2024-11-11 09:09:30

这次我们的解决方案是强制连接使用 tcp(在连接字符串中使用 data source=tcp:MyServer)并在 SQL 框上启用 TCP(哎呀!)..

我仍然没有了解 Cassini 会话如何能够在 IIS 会话未建立的情况下建立连接 :-S

Mark

Our resolution on this occasion was to force the connection to use tcp (using data source=tcp:MyServer in the connection string) and enabling TCP on the SQL box (oops!)..

I still have no idea how the Cassini session was able to establish connection where the IIS session was not :-S

Mark

尬尬 2024-11-11 09:09:30

我认为问题仍然出在连接字符串上。如果您确定,请执行以下步骤并测试

  1. 打开空白记事本并另存为connectionString.UDL
  2. 打开connectionString(刚刚在步骤1中保存)
  3. 在连接选项卡下:键入服务器名称(在我的例子中:DESKTOP-IDSLV81)或IP为XXX .XXX.XXX.XXX,1433,其中 1433 是默认端口,使用各自的用户名和密码并选择数据库(可用数据库位于下拉列表中)
  4. 然后按测试连接,如果成功,请按“确定”按钮。
  5. 在记事本中打开connectionString.UDL 文件。正确的连接字符串就在那里,并与 web.config 中的连接字符串进行比较

I think the issue is still in connection string. If you are sure please do the following steps and test

  1. Open blank notepad and save as connectionString.UDL
  2. Open connectionString (just saved in step 1)
  3. Under the connection tab: type server name (in my case: DESKTOP-IDSLV81) or IP as XXX.XXX.XXX.XXX,1433 where 1433 is default port the use respective username and password and the select Database(available DB is in Dropdown list)
  4. Then press test connection if succeeded the press Ok button.
  5. Open the connectionString.UDL file in notepad. The correct connection string is there and compare with your connection string in web.config
甜柠檬 2024-11-11 09:09:30

此解决方案适用于在部署Windows应用程序时遇到此问题的人,

我知道这已经晚了,但这可能对将来的某人有用,在我的场景中,纯粹是我开发了一个Windows的连接字符串问题使用实体框架(数据库优先方法)的应用程序并且我发布了它,代码在我的计算机上运行良好,但在客户端计算机上不起作用

此问题的原因:-

我更新了客户端计算机连接App.config 文件中的字符串,但这是错误的,如果这是一个 Windows 应用程序,那么它将不会读取来自 App.config 的连接字符串(对于已部署的计算机),它将从 .exe.config 文件中读取,

因此在部署后,我们需要更改“AppicationName”.exe.config 文件中的连接字符串

This solution is for someone facing this issue while deploying windows application,

I know this is late, but this maybe useful for someone in future, In my scenario, purely this is a connection string issue I developed a windows application using entity framework (DB First approach) and I published it, the code was worked fine on my machine, but it's not worked on the client machine

Reason for this issue:-

I updated the client machine connection string in App.config file, but this is wrong, if that is a windows application, then it will not read the connection string from App.config (For deployed machines), it will read from .exe.config file,

So after deployment, we need to change the connection string in "AppicationName".exe.config file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文