Home > Mobile >  'No connection string named 'MyEntities' could be found in the application config fil
'No connection string named 'MyEntities' could be found in the application config fil

Time:12-12

I have a solution which has two projects, a .NET Web API and a .NET class library. I have used ADO.NET Entity Framework to generate the .edmx for my class library project.

I have also referenced the class library project and entity framework in my web API, so I have no issue referencing the correct models etc. (when creating a controller). I have also ensured that entity framework is installed for both the Web API and the Class Library (using Nuget package manager).

However, I have a simple controller that should return all of the records in a specified table. However, whenever this controller is called, I receive the following error:

'No connection string named 'VWRoodepoortEntities' could be found in the application config file.'

At first, my Web API had no .config file, so I added a web.config template and added the necessary connection string (the same connection string found within the app.config of the class library). My web.config now looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="VWRoodepoortEntities" connectionString="metadata=res://*/VWRoodepoortModel.csdl|res://*/VWRoodepoortModel.ssdl|res://*/VWRoodepoortModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog=VWRoodepoort;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
    </connectionStrings>
</configuration>

However, despite adding the connection string in the application config file (as requested by the error), I still get the exact same error. I then set the Web API as my startup project, However, I still get the same error. Is there something wrong with my connection string? Any help is appreciated.

EDIT:

I have also added my connection string to my appsettings.json file in my Web API. This file now looks as follows:

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

CodePudding user response:

check in your web.config the name of the connection string must be with "My Entities."

CodePudding user response:

// Try this.
1)string apiURL = ConfigurationManager.AppSettings["MyEntities"].ToString();
2)public static string ConnectionString = string.Empty;
ConnectionString = ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString;

// App.config -- Change connection name from "VWRoodepoortEntities" to "MyEntities".

<connectionStrings> <add name="MyEntities" connectionString="server=servername;port=3306;database=my_database;uid=root;password=*****;SslMode=none" /> </connectionStrings>

  • Related