0
votes

In vs.net I have multiple project in one solution, webApplication is startup project (asp.net core 2.1) and DataAccess project (Class library .net framework 4.6), I copied connection string from app.config (DataAccess project) to appsettings.json in webApplication but on access DB error ocurred: No connection string named 'Hemmat_Entities' could be found in the application config file.

app.config in DAL project:

 <connectionStrings>
      <add name="Hemmat_Entities" connectionString="metadata..."
 </connectionStrings> 

appsettings in asp.net core is:

{
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Hemmat_Entities": "data source=.;initial 
           catalog=hemmat_new;integrated 
             security=True;multipleactiveresultsets=True;application 
             name=EntityFramework&quot;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  }
}
3
Can you show the code you use to read the connection string? - stuartd
No code is used to read the connection string, just I add it to appsettings.json, DataAccess layer reads connection string from app.config and it's work fine, but when access from webApp project it gives error: No connection string named 'Hemmat_Entities' could be found - Hojjat Norouzi
You need the connectionString entry in the web.config for the DAL in the asp.net core project (not that it comes with one but you can have one) or update DAL to .net core - mvermef
You mean to add web.config to asp.net core project? How can do this? just copy or add from new item? - Hojjat Norouzi

3 Answers

1
votes

I found solution, my mistake was that Asp.net core 2 project targeted on .net core, after change it to .net framework, problem solved!

so if we want to reference to other projects running under .net461, main project in my case asp.net core 2 must be in .net framework target mode.

0
votes

Try getting the value of Hemmat_Entities using GetValue. And : is needed for nested value.

string connectionString = Configuration.GetValue<string("ConnectionStrings:Hemmat_Entities");

0
votes

@t-prisar. You need inject one by Presentation Layer, passing to DAL Constructor.