Entity SQLCE Can't find connection string in app.config file

*爱你&永不变心* 提交于 2020-01-04 04:14:27

问题


I've been getting this error when I try and use my model container:

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

I have my edmx file in a separate project. I checked the app.config file and my model was there, and I also put it in my main project app.config file. Still doesn't work. Here's the connection string:

    <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>

Here's how the context is called:

Private mdbContext As New PFModelContainer

Which goes to:

Partial Public Class PFModelContainer
    Inherits DbContext

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

I thought the answer would be similar to what happened to this guy. But unfortunately his solution doesn't work with mine.

Update:

I've noticed the error isn't caught until I hit this code. It occurs when I do the linq query on the third line.

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

Update (What I've tried for connection strings - that I can remember):

1 Main Project: --> Default Creation

<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" />

Library:

<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 Main Project: --> Replace * with PFDatabase

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

Library: [...Same w/ modifications...]

3 Main Project: --> Replace res://*/ with .\

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

Library: [...Same w/ modifications...]

4 Main Project: --> Replace res://*/ with ~\

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

Library: [...Same w/ modifications...]


回答1:


If you are placing your edmx model in a separate class library, add an app.config to that class library and add the connection string to that config.

Additionally, if your datamodel resides inside a namespace then you will have to include the full namespace in your resource path:

For example, if you placed your edmx file in an assembly called MyProject.DataLayer and the namespace for the generated code is MyProject.DataLayer.DataModel, then your configuration string should be:

<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" />



回答2:


Got some help from experts exchange on this one. Ended up doing a work around (not sure why it wasn't working like it should), since I'm using DbContext instead of EntityObject I had to create my own overrideable procedure like (the second one below):

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

Public Sub New(ByVal connectionString As String)
   MyBase.New(connectionString)
End Sub

I then had to create my own connection string, which is basically what the original code generated, so, I'm not sure why it wasn't working from the app.config file. Maybe there's a bug in the program that will be fixed the next go around? Hopefully that was it, either that or I did something wrong, mystery.



来源:https://stackoverflow.com/questions/7493337/entity-sqlce-cant-find-connection-string-in-app-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!