Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 386
Jon49 Avatar asked Dec 06 '25 06:12

Jon49


1 Answers

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" />
like image 193
Maciej Avatar answered Dec 08 '25 19:12

Maciej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!