Solved

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

Posted on 2011-09-22
10
1,322 Views
Last Modified: 2013-12-25
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>

Open in new window


Here's how the context is called:

Private mdbContext As New PFModelContainer

Open in new window


Which goes to:

Partial Public Class PFModelContainer
    Inherits DbContext

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

Open in new window


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

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

Open in new window

0
Comment
Question by:lilypad49
  • 6
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36587428
The connection string needs to be in the entry assembly project's app.config.
0
 

Author Comment

by:lilypad49
ID: 36587473
Yes, I've tried that and it still doesn't work, this is what I put:


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

Open in new window


I also tried:

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

Open in new window


and

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

Open in new window


and

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

Open in new window


What am I missing? Should it be written differently?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36587497
You can use the System.Configuration.ConfigurationManager to try to get the connection string.  Also, you can construct a data context, by passing in the connection string, since there are overloaded constructors.
0
 

Author Comment

by:lilypad49
ID: 36590079
I imported System.Configuration then did

dim con as Configuration
dim con as Configuration.ConfigurationSettings

But VB didn't like it, ConfigurationManager didn't even pop up. I'm using VB.NET Express, so I don't know if that is part of the problem.

I'll try and do the construct with data context.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36590099
That would be something like this:

Import System.Configuration

...

Dim connectionString As String = ConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
Dim context As New MyDataContext(connectionString)

Open in new window


where "MyDatabase" in the connection string.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:lilypad49
ID: 36590445
OK, I finally got a connection string working using

Using conn As EntityConnection = New EntityConnection(entityBuilder.ToString)

Open in new window


from http://msdn.microsoft.com/en-us/library/bb302786.aspx

but when I try do use

Dim context As New MyDataContext(connectionString)

Open in new window


it gives me the error:


Too many arguments to 'Public Sub New()'.

Am I supposed to create the overloaded constructors myself or am I missing something.

TheLearnedOne: Thanks for your patience with me!
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36590514
I was thinking Entity Framework, but it looks like you are using a different platform for your data context.
0
 

Author Comment

by:lilypad49
ID: 36590623
I am using Entity Framework v. 4.1. To grab the context I use:

I was using

ADO.NET DbContext Generator

was I supposed to be using

ADO.NET EntityObject Generator

?

What's the difference between the two?
0
 

Author Comment

by:lilypad49
ID: 36590690
OK, I looked up the difference, I guess EntityObject is the old way of doing things and DbContext is the new "preferred" way of doing things.

So, apparently inside of the dbcontext mybase.new can either be the name of the context or the connection string, unfortunately they didn't put in an overrideable statement (someone must have missed it), so I did it manually. It works now!

Thanks so much for your help oh learned one! I just wonder why it wasn't working properly, I would think they would just make it plug and chug, I wonder what I'm doing wrong :\ .
0
 

Author Closing Comment

by:lilypad49
ID: 36590697
Thanks once again for your help! It would be nice to find out what was going wrong with Microsoft (or me)!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now