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.

Community
  • 1
  • 1
Guarav T.
  • 468
  • 6
  • 20
  • 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. – Xeun Jul 29 '16 at 08: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 http://go.microsoft.com/fwlink/?LinkId=260883)' So is it still good to use the same approach as in link http://stackoverflow.com/questions/10266923/how-to-share-a-connection-string-between-multiple-entity-data-model or there are better way to do it. – Guarav T. Jul 29 '16 at 08: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 – Xeun Jul 29 '16 at 08:16
  • edit: I guess i got your question wrong. Your are probably better with Bassams approach. – Xeun Jul 29 '16 at 09:41
  • @Xeun: No problem, Thanks for your help :-) – Guarav T. Jul 29 '16 at 09:53

2 Answers2

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!

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • 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. – Guarav T. Jul 29 '16 at 09:52
  • 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 Alugili Jul 29 '16 at 10:06
  • @ Bassam: The problem I was getting is due to reason: http://stackoverflow.com/questions/12622408/no-connection-string-named-myentities-could-be-found-in-the-application-config 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. – Guarav T. Jul 29 '16 at 10:13
  • 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 you need to add the file connectionStrings.config should be in the same directory where you have your config files Relese\MyWeb.Config => here also the connection string. – Bassam Alugili Jul 29 '16 at 14:03
0

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

Guarav T.
  • 468
  • 6
  • 20