I have a Dll which uses the EntityFramework to access data. I have the connection string specified within the dll but I also need to put the same connection string in the app that consumes the dll for some reason.... Is there any way of preventing this annoying
behaviour so that the dll uses its own connection string?
You can use in the app.config of your project host the same key of your connectionstring, and using in ObjectContext.
Sample:
ClassLibraryTest.ModelEntities entities = new ClassLibraryTest.ModelEntities(System.Configuration.ConfigurationManager.ConnectionStrings["ModelEntities"].ConnectionString);
var stores = from s in entities.Store
select s;
I hope this help.
Kindly mark this post as "Answer", if it helped you.
This does not work as the configuration manager looks in the web.config of the application consuming my dll rather than the app.config of the dll itself.
The ConfigurationManager is commum to web.config and app.config, but the error is because the resources in the connectionstring should reference to the assembly.
My sample in ConsoleApp with ClassLibrary referenced:
I have tried this but it errors telling me that the connection string is null (which obviously it is in the web.config but not the app.config of the dll)
I am using the following connection string in the app.config of my dll:-
You have your Entity Framework in a classlibrary (MyAPI), in this classlibrary has the file MyAPI.dll.config that have the connectionstring that works.
When you reference the MyAPI in your web project, you need to copy the connectionstring of MyAPI.dll.config to web.config and alter the resources.
coolblue
Participant
1087 Points
502 Posts
Multiple Connection Strings for EnitiyFramework
Apr 25, 2012 02:57 PM|LINK
I have a Dll which uses the EntityFramework to access data. I have the connection string specified within the dll but I also need to put the same connection string in the app that consumes the dll for some reason.... Is there any way of preventing this annoying behaviour so that the dll uses its own connection string?
pierrefrc
Participant
947 Points
201 Posts
Re: Multiple Connection Strings for EnitiyFramework
Apr 25, 2012 05:34 PM|LINK
Hi
You can use in the app.config of your project host the same key of your connectionstring, and using in ObjectContext.
Sample:
ClassLibraryTest.ModelEntities entities = new ClassLibraryTest.ModelEntities(System.Configuration.ConfigurationManager.ConnectionStrings["ModelEntities"].ConnectionString); var stores = from s in entities.Store select s;I hope this help.
coolblue
Participant
1087 Points
502 Posts
Re: Multiple Connection Strings for EnitiyFramework
Apr 30, 2012 10:58 AM|LINK
This does not work as the configuration manager looks in the web.config of the application consuming my dll rather than the app.config of the dll itself.
pierrefrc
Participant
947 Points
201 Posts
Re: Multiple Connection Strings for EnitiyFramework
Apr 30, 2012 11:45 AM|LINK
Hi
The ConfigurationManager is commum to web.config and app.config, but the error is because the resources in the connectionstring should reference to the assembly.
My sample in ConsoleApp with ClassLibrary referenced:
I hope this help
coolblue
Participant
1087 Points
502 Posts
Re: Multiple Connection Strings for EnitiyFramework
May 29, 2012 03:16 PM|LINK
I have tried this but it errors telling me that the connection string is null (which obviously it is in the web.config but not the app.config of the dll)
I am using the following connection string in the app.config of my dll:-
<add name="dbEntities" connectionString="metadata=res://MyAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;User Id=root;password=ertyyyyh123;database=db"" providerName="System.Data.EntityClient" />
I am then calling the context from my dll as follows:-
using (var context = new smegdbEntities(System.Configuration.ConfigurationManager.ConnectionStrings["dbEntities"].ConnectionString))
{
}
as soon as I reinstate the connection string in my web.config everything works again.
pierrefrc
Participant
947 Points
201 Posts
Re: Multiple Connection Strings for EnitiyFramework
May 29, 2012 03:41 PM|LINK
Hi
Can try:
coolblue
Participant
1087 Points
502 Posts
Re: Multiple Connection Strings for EnitiyFramework
May 29, 2012 03:59 PM|LINK
Still does not work as it says the connectionstring is null
pierrefrc
Participant
947 Points
201 Posts
Re: Multiple Connection Strings for EnitiyFramework
May 30, 2012 12:16 AM|LINK
Hi coolblue
I think that don't understand your problem.
You have your Entity Framework in a classlibrary (MyAPI), in this classlibrary has the file MyAPI.dll.config that have the connectionstring that works.
When you reference the MyAPI in your web project, you need to copy the connectionstring of MyAPI.dll.config to web.config and alter the resources.
Sample:
<add name="dbEntities" connectionString="metadata=res://MyAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;User Id=root;password=ertyyyyh123;database=db"" providerName="System.Data.EntityClient" />
Whether you don't want to use this connectionstring in your web.config and you want to use the connectionstring in MyAPI.dll.config use in call of EF: