2

I have 3 different project having their respective EF entity data model pointing to same database.I don't want to save connection string in each of these project's app.config file but want to share it between my models.

I see this link on stackoverflow How to share a connection string between multiple entity data model.

But the problem with it is if I will update the EF model it will overwrite the code in EF Model's context and it will inherit from DbContext not from BaseContext.

Please help how can I resolve this.

5
  • The class generated by EF is partial. Just put the modified constructor as well as the "helper" code into another file which extends the generated one. This way you do not need to worry about EF overwriting your custom code. Commented Jul 29, 2016 at 8:09
  • @Xeun: Thanks for prompt response but now when I trying to run the example in .Net Framework 4.5 I am getting warning Warning CS0618 'Database.DefaultConnectionFactory' is obsolete: 'The default connection factory should be set in the config file or using the DbConfiguration class. (See go.microsoft.com/fwlink/?LinkId=260883)' So is it still good to use the same approach as in link stackoverflow.com/questions/10266923/… or there are better way to do it. Commented Jul 29, 2016 at 8:11
  • I am not a huge fan of the solution you showd within the answers. In your case it is just the connection factory which is obsolete, let me show you a code example, i will write an answer Commented Jul 29, 2016 at 8:16
  • edit: I guess i got your question wrong. Your are probably better with Bassams approach. Commented Jul 29, 2016 at 9:41
  • @Xeun: No problem, Thanks for your help :-) Commented Jul 29, 2016 at 9:53

2 Answers 2

0

You have to move your connection string in a separate config file:

ConnectionStrings.config

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="connectionString"
       connectionString="Integrated Security=SSPI; Persist Security Info=False; Initial Catalog=DbName; Data Source=.\SQLExpress;"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Modify the connection string so that fit your requirement.

Then you can share it with all your projects like that:

1) Open your App.config (This file found in your project)

2) Add this line code somewhere behind </configSections>

 ...
 <connectionStrings configSource="ConnectionStrings.config"/>
 ...

The trick in configSource:

"Gets or sets the name of the include file in which the associated configuration section is defined, if such a file exists." https://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource(v=vs.110).aspx

What will happened:

  • ConnectionStrings.config must be first copied

  • All YourApplicationName.config will reference the shared connection string config file.

If the project does not have any App.config then just add it! or you can also loaded manually with ConfigurationSettings.

This is the best way to share the database configuration between the app.configs and when you change for example the Sql Server name, then you have only to modify the ConnectionStrings.config and not all App.configs!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for replying but in my project ConnectionStrings.config file is in a common DAL project and different projects for each EF data model. So what will be the path of the file in app.config file. Please suggest. I am getting error: Additional information: No connection string named 'ABC_Entity' could be found in the application config file.
for prof of concept that everything is working just put ConnectionStrings.config wihout any path and copy it manually to your bin\Release or Debug directory. when you are sure it is working then add the ConnectionString.config to your infrastructure assebmly and select in in Properies window Copy To OutPut Directory | Copy Always or try to put it somwhere in root and referance it with ..\..\... --> im not sure how this referceing can work but you have to try it.
@ Bassam: The problem I was getting is due to reason: stackoverflow.com/questions/12622408/… But when I did it then even after removing the connection string from app.config files of my project its picking connection string from web project. So did this resolve my problem or can I have some issues. Please suggest.
If you have configure your app.config or web.config correctly then everything should working and you should not get this error message. you need in your web.config/app.config file section = "entityFramework"... do you have ? and then after the tag </configSections> you need to add <connectionStrings configSource="connectionStrings.config"/> the file connectionStrings.config should be in the same directory where you have your config files Relese\MyWeb.Config => here also the connection string.
0

It resolved as connection string always picked from MVC project and all other class library projects are referencing it automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.