Entity Framework connections strings messing up app.config - c#

I'm using Entity Framework inside a DLL.
VS creates a app.config, but when I run the application I receive this exception.
"The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."

You need to include that connection string in the start up project of your solution. If it is a Asp.Net WebForms/MVC application include that connection string in the Web.Config file or if it is a Windows or Console App include it in App.Config file.

Related

Connection string changes not picked up by ConfigurationManager

I'm having a issue with ConfigurationManager not picking up a change to a connection string.
I'm sure there's a simple explanation, however I've been staring at it for the last two hours and given myself contextual blindness.
Inside a WCF project I've updated the database name of a named connection string in the web.config:
<connectionStrings>
<add name="appDb"
connectionString="Data Source=HOST;Database=DB_NEW;User=X;Password=X;"
providerName="System.Data.SqlClient" />
</connectionStrings>
There's a separate repository project that orchestrates the database connection, abstracting it from the WCF project. In that project it uses the connection string value from the web.config file, accessed using the following expression:
ConfigurationManager.ConnectionStrings["appDb"].ConnectionString
(The WCF project manages dependency injection using Unity and is responsible for creating an instance of the session factory in the referenced project for NHibernate's ISessionFactory.)
However, at runtime it's still picking up the connection string prior to me changing it.
Things I've tried:
Full clean/rebuild of the entire solution
iisreset
Checked IIS physical path is the correct directory (I have multiple branches of the same project)
Checked the connection string isn't being defined anywhere else in the solution
Restarted Visual Studio (2015)
No transforms being applied to the web.config
The connection string value must be being cached somewhere, but where I do not know.
Update
Could this be NHibernate caching the connection string? Apologies I have very little knowledge with NHibernate (I'm usually EF).
var config = new NHibernate.Cfg.Configuration()
.SetProperty(Environment.ConnectionString,
ConfigurationManager
.ConnectionStrings["AppDb"].ConnectionString)
Update
IIS seems to recognise the new config in IIS Manager, so this confirms that it's being read from the file correctly.

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

I have a class library with a method that takes a string and returns an xml document. This library references another project - containing edmx models generated with EF6 db first.
I realize that my connection strings from the EF project must be moved into the "executing" application config file - this is fine in development as I have created a simple web client as my startup project and it's web.config contains the necessary connection strings.
When I deploy this solution to my QA server, the "executing" application is actually a VB6 app and I am getting this error. I have no idea what application config file is being used in this case.
Any suggestions?

Forcing Entity Framework and ASP.MVC to use the connection string from app.config of another assembly

Set & settings:
I use Entity Framework 5 and have a dll project with edmx file. In this project I have App.config with connection string for the EF model. I have also second project, ASP.MVC 4 web application which is a startup project. It references the database project. Important thing is - db is Oracle and EF uses Oracle custom providers.
Problem:
If I place my connection string in the ASP.MVC startup project is works fine. It's common advice to do this. But I don't want to. I don't see reason why I should. How can I force MVC/EF to find the connection string in App.config of the external library (which as a matter of fact is a data access layer)?
App.Config is used by WinForms, WPF and executable applications.
Web.Config is used in IIS (and is able to set IIS environment specific configurations)
It seems no App.Config will ever be merged to the Web.Config (source):
In using an App.config, file the configuration system merges the
App.config file with content of the Machine.config file when the
application starts and the configuration is applied. This mechanism
allows machine-wide settings to be defined in the Machine.config file.
The App.config file can be used to override the settings of the
Machine.config file; you can also lock in the settings in
Machine.config file so that they get used. In the Web.config case, the
configuration system merges the Web.config files in all directories
leading up to the application directory into the configuration that
gets applied. For more information about configuration and the setting
priorities, see topics in the System.Configuration namespace.
Perhaps you'll find a solution more appropriate to your needs by using Application Configuration Files.
Finally, after facing multiple issues, I've decided to move connection string of DAL into the Web.config of the web application. I was convinced by some arguments you can read here in the post of Chris Ammerman.

C# linq to sql access app.config file

I am having a linq to sql problem with a C# 2010 windows form application when it is changed to a dll and accessed by other applications. The purpose of the calling the code as a dll is for performance reasons and to share the code accross multiple applications.
When the application is on its own, it runs fine.
Basically when the linq to sql was dragged to the desktop, a default linq to sql connection was setup by the .net framework. However I changed the *.designer.cs to use the system.configuration namespace,
added a reference to the system.configuration namespace, and changed the default linq to sql connection to use the database connection obttained in app.config file. I also removed any refernces to the default database connections in the the 'properties or 'settings' section for each project file folder.
Now the problem is the default setting values to the database are reset by the .net framework. Thwe values in the app.config file are no longer being used.
Can you show me code, point me to a reference, and/or tell how I can make this application is the connection string values in the app.config file again?
A dll is run in the the context of the application calling it. It is not an application in its own right, so giving it an app.config file is useless. The configuration settings used by the dll are supplied by the calling application's .config file (it can be an app.config file or a web.config file). In your case, any application using the dll must have the database connection defined its own .config file.

ArgumentException was unhandled

Here's a screenshot of the error:
The error itself is:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Going to the app.config file of that same project I can find:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DocumentsEntities" connectionString="metadata=res://*/Documents.csdl|res://*/Documents.ssdl|res://*/Documents.msl;provider=System.Data.SQLite;provider connection string='data source="D:\Programming Projects\Desktop Applications\C#\DocumentScanner\DAL\Documents.sqlite"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
The connection string does exist but I'm getting this error.
I'm using SQLite and .NET Framework 3.5.
Any suggestions?
Have you seen this answer? Does it help?
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/f5904b4d-b2f8-421e-90de-339f93959533/
Quoting:
My only guess is the app.config is not
in the binary directory where the exe
is. Please do the following:
1. Visually verify that the app.config with the expected content
is in the directory where the exe is
compiled. (Existence in the project
root directory is not enough.)
2. Use System.Configuration.ConfigurationManager
from within your app to examine the
content of the app.config your exe is
using. I’m also looking at the
content of the connection string, and
I can say that it may not work in a
multi project environment (unless
you’ve duplicated the EDM in each
project). The reason for that is “.”
resolves to the directory where the
exe is loaded from. If you want to
reuse the same EDM, you at least have
to make a few steps back in the path
and then navigate to the project where
the EDM is, e.g.
“......\Proj1\AdventureWorksModel”.
Additionally you may consider using
the |DataDirectory| macro - when you
load an AppDomain you can set
|DataDirectory| to point to the exact
directory where the EDM is, and then
use that in the connection string,
e.g.
“|DataDirectory|\AdventureWorksModel”.
If you are working on an ASP.NET
project, you can use “~” which refers
to the project root. In that latter
case, you can’t reference a model
outside your project’s hierarchy
though.
Zlatko Michailov Program Manager,
Data Programmability Runtime Microsoft
Corp.

Categories