vb.net - 实体 SQLCE 在 app.config 文件中找不到连接字符串

标签 vb.net linq entity-framework entity-framework-4.1 sql-server-ce

当我尝试使用我的模型容器时,我遇到了这个错误:

No connection string named 'PFModelContainer' could be found in the application config file.

我的 edmx 文件在一个单独的项目中。我检查了 app.config 文件,我的模型在那里,我也把它放在我的主项目 app.config 文件中。还是不行。这是连接字符串:

    <connectionStrings>
        <add name="PFModelContainer" 
             connectionString="metadata=res://*/PFModel.csdl|res:
                               //*/PFModel.ssdl|res://*/PFModel.msl;
             provider=System.Data.SqlServerCe.3.5;
             provider connection string=&quot;
             Data Source=C:\Documents and Settings\Jon\My Documents\Visual
                         Studio 2010\Projects\SpreadsheetAddIn
                         \SpreadsheetAddIn\bin\Debug\PFData.sdf;
             Password=password&quot;" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>

上下文是这样调用的:

Private mdbContext As New PFModelContainer

去往:

Partial Public Class PFModelContainer
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=PFModelContainer")
    End Sub

我认为答案与 happened to this guy 类似.但不幸的是,他的解决方案不适用于我的。

更新:

我注意到在我点击这段代码之前,错误没有被捕获。它发生在我在第三行执行 linq 查询时。

Dim dbContext As New PFModelContainer
Dim dbAccount As IQueryable(Of Account)
dbAccount = From a In dbContext.Accounts
            Where (a.AccountName = "Hello")
            Select a

更新(我对连接字符串的尝试 - 我记得):

1 主项目: --> 默认创建

<add name="PFModelContainer" 
     connectionString="metadata=res://*/PFModel.csdl|
                                res://*/PFModel.ssdl|
                                res://*/PFModel.msl;
     provider=System.Data.SqlServerCe.3.5;
     provider connection string=&quot;
       Data Source=C:\Documents and Settings\Jon\My Documents\Visual Studio 2010\Projects\SpreadsheetAddIn\PFDatabase\bin\Debug\PFData.sdf;
       Password=password&quot;" 
     providerName="System.Data.EntityClient" />

图书馆:

<add name="PFModelContainer" 
     connectionString="metadata=res://*/PFModel.csdl|
                                res://*/PFModel.ssdl|
                                res://*/PFModel.msl;
     provider=System.Data.SqlServerCe.3.5;
     provider connection string=&quot;
     Data Source=|DataDirectory|\bin\Debug\PFData.sdf;
       Password=password&quot;" 
       providerName="System.Data.EntityClient" />

2 主要项目: --> 用 PFDatabase 替换 *

<add name="PFModelContainer" 
     connectionString="metadata=res://PFDatabase/PFModel.csdl|
                                res://PFDatabase/PFModel.ssdl|
                                res://PFDatabase/PFModel.msl;
                                [...Same...]

图书馆: [...与修改相同...]

3 主要项目: --> 将 res://*/替换为 .\

<add name="PFModelContainer" 
     connectionString="metadata=.\PFModel.csdl|
                                .\PFModel.ssdl|
                                .\PFModel.msl;
                                [...Same...]

图书馆: [...与修改相同...]

4 主要项目: --> 将 res://*/替换为 ~\

<add name="PFModelContainer" 
     connectionString="metadata=~\PFModel.csdl|
                                ~\PFModel.ssdl|
                                ~\PFModel.msl;
                                [...Same...]

图书馆: [...与修改相同...]

最佳答案

如果您将 edmx 模型放在单独的类库中,请将 app.config 添加到该类库并将连接字符串添加到该配置。

此外,如果您的数据模型驻留在命名空间内,那么您必须在资源路径中包含完整的命名空间:

例如,如果您将 edmx 文件放在一个名为 MyProject.DataLayer 的程序集中,并且生成的代码的命名空间是 MyProject.DataLayer.DataModel,那么您的配置字符串应该是:

<add name="PFModelContainer"
         connectionString="metadata=res://*/DataModel.PFModel.csdl|res:
                           //*/DataModel.PFModel.ssdl|res://*/DataModel.PFModel.msl;
         provider=System.Data.SqlServerCe.3.5;
         provider connection string=&quot;
         Data Source=C:\Documents and Settings\Jon\My Documents\Visual
                     Studio 2010\Projects\SpreadsheetAddIn
                     \SpreadsheetAddIn\bin\Debug\PFData.sdf;
         Password=password&quot;"
         providerName="System.Data.EntityClient" />

关于vb.net - 实体 SQLCE 在 app.config 文件中找不到连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493337/

相关文章:

c# - 使用 Entity Framework 6 代码优先注释映射 xmltype oracle

c# - 如何使用 LinQ 对特定数量的行进行分组?

c# - 为什么在表达式树中需要转换

c# - 如何检查表中是否存在特定类的实例?

c# - 使用 Debug模式而不是 Release模式将应用程序部署到生产环境?

c# - Linq 查询在本地机器上运行良好,但在服务器上出现 "The connection was reset"错误

c# - Enumerable.Range(...).Any(...) 优于基本循环 : Why?

.net - 用两个单引号分割字符串

xml - 指定超过 1 个 Xml 根属性

c# - 从 Android 中的 ASPX 页面获取响应