Doesn't work with EntityFramework\update-database
I've got an ASP.NET MVC application with entity framework code first approach.
I want to hide a connection string in a web.config file from being tracked by a git repository.
...
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="JsonConfig" jsonFile="~\web.config.json" jsonMode="Sectional" ignoreMissingFile="true" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Culture=neutral" />
</builders>
</configBuilders>
<connectionStrings configBuilders="JsonConfig">
<add name="DbTest" connectionString="__DbTest__" providerName="System.Data.SqlClient" />
</connectionStrings>
...
I use web.config.json file that is ignored by the git repository to keep the connection string.
{
"connectionStrings": {
"DbTest": "server=.;database=DbTest; User Id=DbTest.login; Password=DbTest.Password"
},
...
}
It is stated that properly configured SimpleJsonConfigBuilder should match keys from both files and substitute fake values in web.config file with proper ones from a json file. In most cases it does so but when I try to execute EntityFramework\update-database command to roll changes out I get the following error:
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.
b__18(DbConnection t, DbConnectionPropertyInterceptionContext 1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext1 interceptionContext) at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection) at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config) at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.get_Connection() at System.Data.Entity.Internal.LazyInternalContext.get_Connection() at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() Format of the initialization string does not conform to specification starting at index 0.
It looks like the DbContext that does a migration obtains a fake value instead of an actual one.
Any ideas how to workaround this issue?
I'm not an expert on EF... but from a quick glance at the EF6 codebase, it does look like they are going through ConfigurationManager.ConnectionStrings to get the connection info. Which means config builders should apply.
However, I suspect that what is happening here is that the migration utility can't find "~\web.config.json" when it applies your JsonConfig builder. The '~' at the start of the path gets special handling in config builders. If the config builder is running in the context of an ASP.Net app, it will let Server.MapPath() handle translation of the tilde into an actionable path. Otherwise it substitues AppDomain.CurrentDomain.BaseDirectory, and this might not be the directory you expect.
An easy way to check this would be to set ignoreMissingFile to false and see if you get an exception from configuration instead of System.Data.
@aliaksandr-valchuk Did you ever find a workaround without needing to code the absolute path to the file? I'm currently experiencing the same issue.
Why is this issue closed? From my experience, a local update from PS, when you actually have config builders in our web config provides an error.
It was closed because there didn't seem to be a bug here with config builders. A path was given to a config file that probably wasn't being found. To verify that theory, a setting was suggested which would help confirm this was the case. There was no response to that suggestion... so I closed the issue.
Now, on the surface if the Json config builder can't find the Json file, then there really isn't anything we can do here. Looking a little deeper though, there is the magic of "~" to consider. And as it turns out, "~" handling was a little fragile/broken at the time. It should be more accurate and robust now. (#170)
At the end of the day though, if the builder can't find the source Json, there isn't much else to do here. ignoreMissingFile = false is how to confirm the issue.