'No connection string named 'foobar' could be found in the application config file. Already set to startup and default projects

I am using entity framework 6.4.4 and attempting to use the Update-Database command No connection string named 'OneOrder Connection String' could be found in the application config file. Already set C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External to startup and default projects

I couldn't find a fix for this issue that wasn't just setting the startup project and default project to the correct one. The only thing I can think of at this point is this won't work with net6.0 and I'll need to refactor to use entityframework core

Here is the top of my proj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <OutputType>Library</OutputType>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

Here is the full verbose error:

C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\bin\Debug\OneOrder.DataAccessComponent.External.deps.json --additionalprobingpath C:\Users\as\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\bin\Debug\OneOrder.DataAccessComponent.External.runtimeconfig.json C:\Users\as\.nuget\packages\entityframework\6.4.4\tools\netcoreapp3.0\any\ef6.dll database update --verbose --no-color --prefix-output --assembly C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\bin\Debug\OneOrder.DataAccessComponent.External.dll --project-dir C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\ --language C# --root-namespace OneOrder.DataAccessComponent.External --config C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\App.config
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.InvalidOperationException: No connection string named 'OneOrder Connection String' could be found in the application config file.
   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.Infrastructure.DbContextInfo..ctor(Type contextType, Func`1 resolver)
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType)
   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.Infrastructure.Design.Executor.CreateMigrator(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Infrastructure.Design.Executor.UpdateInternal(String targetMigration, Boolean force, DbConnectionInfo connectionInfo, String migrationsConfigurationName)
   at System.Data.Entity.Infrastructure.Design.Executor.Update.<>c__DisplayClass0_0.<.ctor>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
No connection string named 'OneOrder Connection String' could be found in the application config file.

Here is the config file that is defined by --config C:\Git-Projects\OneOrder\src\OneOrder.DataAccessComponent.External\App.config and confirmed the one it is trying to use. I have tried various changes in config but the connection string still won't get picked up.

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />    
  
  </configSections>
  <connectionStrings>
    <add name="OneOrder Connection String" connectionString="Data Source=.;Database=OneOrder;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.;Integrated Security=SSPI;MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Lastly here is the context file constructor:

  public OrderContext() : base("name=OneOrder Connection String")
    {

    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source