1

Someone thought, a long time ago, it was a good idea to add connections string hardcoded in the datalayer of our web api. Because of this legacy I cannot remove this class. This class inherits IDbContextFactory and it needs to retrieve a connection string, which now is hard coded. To make it more... dynamic, I want to use the Azure connection strings for this.

1 I added the connection string to the configuration of the app service

2 According to a lot of websites I can just add the following code to retrieve the connection string:

connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

This does not work; it gives a "Object reference not set to an instance of an object." error.

Other say you need to add a prefix and the environment:

connectionString = Environment.GetEnvironmentVariable("SQLCONNSTR_DefaultConnection");

This does not work either, obviously. And yes, I selected sql server in the dropdown.

Other thing I tried, as someone suggested, is to add the connection string to the web.config. And again; this didn't work.

Good to know is that this is .NET 4.6.2, so all the beautiful solutions for .NET Core 1/2 aren't going to work.

1 Answers1

0

First you have to include the same connection string in the web.config with empty connectionString value. Then you can overwrite from the application settings section.

`<connectionStrings>
    <add name="DefaultConnection" connectionString="" />
    <add name="CMSEntities" connectionString="" 
 providerName="System.Data.EntityClient" />
</connectionStrings>`

For entity framework specify providerName as "System.Data.EntityClient" and settings chose Custom instead of SqlServer

garo cm
  • 61
  • 5
  • When I do that, the web.config is being replaced by one created by my TFS Release or Build (can't really figure out which one). Long story short: connectionStrings dissapear after release. How to prevent this? –  Mar 08 '19 at 10:15
  • Did you add the empty connection string to the config or release config? – garo cm Mar 08 '19 at 10:20
  • I added a web.config to the api project and added the connection string to the config file. I commited it, TFS did some building and releasing and via Kudu I saw the connectionStrings-section was gone. –  Mar 08 '19 at 10:24
  • I'm not familiar with TFS, I think you should revise your question. It seems to be a TFS related problem. – garo cm Mar 08 '19 at 10:31