lilypad49
asked on
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:
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:
Here's how the context is called:
Which goes to:
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.
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="
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>
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.
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
The connection string needs to be in the entry assembly project's app.config.
ASKER
Yes, I've tried that and it still doesn't work, this is what I put:
I also tried:
and
and
What am I missing? Should it be written differently?
<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" />
I also tried:
<add name="PFModelContainer"
connectionString="metadata=res://PFDatabase/PFModel.csdl|
res://PFDatabase/PFModel.ssdl|
res://PFDatabase/PFModel.msl;
[...Same...]
and
<add name="PFModelContainer"
connectionString="metadata=.\PFModel.csdl|
.\PFModel.ssdl|
.\PFModel.msl;
[...Same...]
and
<add name="PFModelContainer"
connectionString="metadata=~\PFModel.csdl|
~\PFModel.ssdl|
~\PFModel.msl;
[...Same...]
What am I missing? Should it be written differently?
You can use the System.Configuration.Confi gurationMa nager 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.
ASKER
I imported System.Configuration then did
dim con as Configuration
dim con as Configuration.Configuratio nSettings
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.
dim con as Configuration
dim con as Configuration.Configuratio
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.
That would be something like this:
where "MyDatabase" in the connection string.
Import System.Configuration
...
Dim connectionString As String = ConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
Dim context As New MyDataContext(connectionString)
where "MyDatabase" in the connection string.
ASKER
OK, I finally got a connection string working using
from http://msdn.microsoft.com/en-us/library/bb302786.aspx
but when I try do use
it gives me the error:
Am I supposed to create the overloaded constructors myself or am I missing something.
TheLearnedOne: Thanks for your patience with me!
Using conn As EntityConnection = New EntityConnection(entityBuilder.ToString)
from http://msdn.microsoft.com/en-us/library/bb302786.aspx
but when I try do use
Dim context As New MyDataContext(connectionString)
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am using Entity Framework v. 4.1. To grab the context I use:
I was using
was I supposed to be using
?
What's the difference between the two?
I was using
ADO.NET DbContext Generator
was I supposed to be using
ADO.NET EntityObject Generator
?
What's the difference between the two?
ASKER
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 :\ .
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 :\ .
ASKER
Thanks once again for your help! It would be nice to find out what was going wrong with Microsoft (or me)!