3

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

Community
  • 1
  • 1
Jon49
  • 4,444
  • 4
  • 36
  • 73
  • Where (a.AccountName = "Hello") probably should be Where (a.AccountName == "Hello"). A single equals sign is an assignment, not a comparison. – Chris Sep 22 '11 at 01:46
  • 1
    That's for C# not VB, I'm using VB. "Assigning and comparing variables uses the same token, =. Whereas C# has separate tokens, == for comparison and = to assign a value" see: http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Visual_Basic_.NET#Other_characteristics_of_Visual_Basic_.NET_not_applicable_to_C.23 – Jon49 Sep 22 '11 at 03:17
  • my bad, I code in C# and it was just natural to notice that as it's a common goof in that world. – Chris Sep 22 '11 at 03:27

2 Answers2

3

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" />
Maciej
  • 2,175
  • 1
  • 18
  • 29
  • There's an app.config in both projects, the library where I created the database (edmx) and my main project. I put the connection string in both just in case. Entity still can't find it though. – Jon49 Sep 21 '11 at 00:35
  • Have you tried to replace the asterisk in the path with the library name like [here](http://blog.ondrejsv.com/post/Entity-Framework-metadata-deployment.aspx)? – Maciej Sep 21 '11 at 00:40
  • Did you replace the asterisk with the name of your assembly name like res://assemblynamewhereyourmodelis/pfmodel.csdl? You will have to replace all asterisks with assemblywhereyourmodelis. – Maciej Sep 21 '11 at 01:16
  • I didn't, I'll work on it again tomorrow and let you know how it works out. Thanks for your help so far! – Jon49 Sep 21 '11 at 01:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3661/discussion-between-maciej-and-jon49) – Maciej Sep 21 '11 at 13:07
  • OK, I was experimenting with different things and didn't get anything to work. I tried what you showed and it didn't work either. I didn't do a namespace just a separate project. I tried using the project name but that didn't work. I'll keep experimenting. If you have any other ideas let me know. It is probably something dumb on my part that I'm doing, but doesn't make sense to me, when I create the database I tested the connection and it works fine. – Jon49 Sep 21 '11 at 20:28
  • Thanks for your help Maciej, I'll give you some extra points for helping me. – Jon49 Sep 24 '11 at 00:10
  • how could i make the data source to application startup path? C:\Documents and Settings\Jon\My Documents\Visual Studio 2010\Projects\SpreadsheetAddIn \SpreadsheetAddIn\bin\Debug\PFData.sdf – r.hamd Apr 04 '16 at 06:47
0

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.

Jon49
  • 4,444
  • 4
  • 36
  • 73