当我尝试使用我的模型容器时,我遇到了这个错误:
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="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual
Studio 2010\Projects\SpreadsheetAddIn
\SpreadsheetAddIn\bin\Debug\PFData.sdf;
Password=password""
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="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual Studio 2010\Projects\SpreadsheetAddIn\PFDatabase\bin\Debug\PFData.sdf;
Password=password""
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="
Data Source=|DataDirectory|\bin\Debug\PFData.sdf;
Password=password""
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="
Data Source=C:\Documents and Settings\Jon\My Documents\Visual
Studio 2010\Projects\SpreadsheetAddIn
\SpreadsheetAddIn\bin\Debug\PFData.sdf;
Password=password""
providerName="System.Data.EntityClient" />
关于vb.net - 实体 SQLCE 在 app.config 文件中找不到连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493337/