Entity Framework Core Connection String - Environment Variable

asp.net asp.net-core c# entity-framework entity-framework-core

Question

I have a .net core app using entity framework core. When running entity framework migrations or updates via the command line, I get a "value cannot be null. Parameter name: connectionString"

The connection string is kept as an environment variable:

ConnectionStrings__SomeContextConnection  ...(localdb)\\MSSQLLocalDB...

However, when I move that exact same connection string into a .json config file:

"ConnectionStrings": {  
    "SomeContextConnection": "...(localdb)\\MSSQLLocalDB..." 
}

Then the entity framework tools recognize the connection string without issue. When debugging the code, in Startup.cs:

var connectionString = _config.GetConnectionString("SomeContextConnection");

the connectionString variable is set to the correct string when the string is stored in either of the two locations, but it crashes when trying to connect to the database when using the environment var.

(note: in the environment variable case, the connection string is escaped so goes from

(localdb)\\MSSQLLocalDB...

to

(localdb)\\\\MSSQLLocalDB...

but the issue persists even after removing the extra back-slashes)

UPDATE: When the connection string is moved into a Windows level environment variable, it works fine. Seems to only be an issue when using Visual Studio environment variables.

1
2
1/26/2017 11:08:49 PM

Popular Answer

It works if you remove the double backslash after (localdb) so that there's only one backslash.

So once you've corrected that in the environment variable it should look something like this:

Server=(localdb)\MSSQLLocalDB...

1
2/12/2017 10:03:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow