简体   繁体   中英

No connection string named 'BACEntities' could be found in the application config file.” when calling 'n .NET Framwork 4.7.2 Library from .NET 5 Api

I have a .NET5 API and would like to use my .NET Framwork 4.7.2 Library that has access to my Database. However, I get the error “No connection string named 'BACEntities' could be found in the application config file.”

The connection string is in the Library's App.Config and it is:

add name="BACEntities" connectionString="metadata=res://*/BACSAModel.csdl|res://*/BACSAModel.ssdl|res://*/BACSAModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=BACSA;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"

I have added the connection string to the .NET 5 appsettings.json file but it didn't work.

Any suggestions?

As far as I know, we should use appsetting.json instead of the App.Config file in asp.net 5 application.

The old .net framework library doesn't have the codes to read the appsetting.json directly.So you should firstly write the codes to read it and then pass it to the .net library sql connection library.

More details, you could refer to below steps:

1.Add Microsoft.Extensions.Configuration to the top of your class (sql connection related class):

using Microsoft.Extensions.Configuration;

2.Add below codes to read the connection string.

var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json").Build();
 var connectionstring =  config["BACEntities"];

3.Modify the appsetting.json like below:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "BACEntities": "metadata=res://*/BACSAModel.csdl|res://*/BACSAModel.ssdl|res://*/BACSAModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=BACSA;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""


}

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM