【问题标题】:2 Connection Strings, The Security String Works, The Entity Framework One Doesn't2 连接字符串,安全字符串有效,实体框架无效
【发布时间】:2015-06-20 20:20:47
【问题描述】:

我是 ASP.NET 新手,发现连接字符串很难掌握。

我已经成功连接了下面的 2 个连接字符串,用于数据库安全的连接字符串工作正常,但我无法让处理其余数据的主字符串工作。我想一定是因为连接字符串的Entity Framework元素。

对于元数据部分,我使用了res://*/ 部分,因为我认为这效率较低,但如果我正确理解了我一直在阅读的文档,它会抓住一切。

<remove name="ApplicationServices" />
  <add name="ApplicationServices" 
     connectionString= "Server=184.168.194.64,1433;
     Database= myASPNETDB;
     User=****;
     Password=****;" 
     providerName="System.Data.SqlClient" />

 <remove name="PropManEntities" />
    <add name="PropManEntities" 
         connectionString="Metadata=res://*/;
                 provider=System.Data.SqlClient;
                 provider connection string='Data Source=184.168.194.64,1433;
         Initial Catalog=myTest_C;
                 User=****;
                 Password=****;
         multipleactiveresultsets=true'" 
                 providerName="System.Data.EntityClient" />

当我打开包含数据的页面时出现以下错误:

The query syntax is not valid. Near line 6, column 17.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.EntitySqlException: The query syntax is not valid. Near line 6, column 17.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[EntitySqlException: The query syntax is not valid. Near line 6, column 17.]
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +964
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.DetailsView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
   System.Web.UI.WebControls.DetailsView.EnsureDataBound() +223
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()+75
   System.Web.UI.Control.EnsureChildControls() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

GoDaddy 不愿意就此事提供任何帮助。

编辑:

在尝试了 Matteo 的建议后,我收到了以下错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.]
   System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) +8425830
   System.Data.EntityClient.EntityConnection..ctor(String connectionString) +43
   System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString) +85
   System.Data.Objects.ObjectContext..ctor(String connectionString) +12
   System.Web.UI.WebControls.EntityDataSourceView.ConstructContext() +461
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +102
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.DetailsView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
   System.Web.UI.WebControls.DetailsView.EnsureDataBound() +223
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()+75
   System.Web.UI.Control.EnsureChildControls() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)+974

【问题讨论】:

  • 您是先使用代码还是从数据库生成 .edmx?
  • 我不认为 Metadata=res://*/ 正在工作,如果是的话,不要认为这真的很方便,因为如果你使用多个它会导致问题,例如来自其他组件的模型。尝试设置正确的 .csdl、.ssdl 和 .msl(如果您不知道它必须是什么,请重新添加一个 edmx 并让它为您创建连接字符串)。
  • 我没看错,您在两个不同的数据库中有数据?
  • @Claies 是的,没错,一个用于处理安全性的 aspnetdb 数据库,一个用于主数据库。我不知道我是否可以将两者结合起来。
  • @MrBliz 我从数据库中生成了一个 .edmx。

标签: c# asp.net .net entity-framework


【解决方案1】:

试试这个

<add name="PropManEntities" connectionString="data source=184.168.194.64,1433;initial catalog=myTest_C;User Id=****;Password=****;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

抱歉,我没有意识到您正在使用 edmx 文件.. 试试这个版本(而不是 ????? 使用你的文件名):

<add name="PropManEntities" connectionString="metadata=res://*/??????????.csdl|res://*/????????????.ssdl|res://*/???????????????.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=184.168.194.64,1433;initial catalog=MPInvoicingSystem;user id=*****;password=*****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

如果这不起作用,我建议您添加一个新的 edmx 模型并复制自动添加的连接字符串。 马特奥

【讨论】:

  • 感谢您的建议,请参阅编辑后的问题,了解我在尝试时收到的新错误消息。谢谢
  • 嗨@Matteo 我一直在尝试理解连接字符串的 .csdl、.ssdl 和 .msl 文件元素。我的印象是这些都包含在 .edmx 文件中。但是,当我编译我的项目时,我注意到包含 .edmx 文件的 DAL 目录不是已编译元素之一。我需要尝试创建这些文件,还是需要更改连接字符串?
猜你喜欢
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
相关资源
最近更新 更多