InvalidOperationException: No connection string named 'PayMyRentEntities' could be found in the application config file

Anonymous
2022-12-20T10:39:09.807+00:00

Working on Migrating .NET Framework to .NET core 6. I am trying to run the application which is not able to read the connection string from appsettings.json file. I have shown the appsettings.json file as well. Please let me know what I need to do. Old application reads from web.config file.

Context File

  public partial class PayMyRentEntities : DbContext  

{

public PayMyRentEntities()  
    : base("name=PayMyRentEntities")  
{  
}  

protected override void OnModelCreating(DbModelBuilder modelBuilder)  
{  
    throw new UnintentionalCodeFirstException();  
}  

public virtual DbSet<Category> Categories { get; set; }  
public virtual DbSet<Hospital> Hospitals { get; set; }  
public virtual DbSet<LookupType> LookupTypes { get; set; }  
public virtual DbSet<LookupValue> LookupValues { get; set; }  

}
Appsetting.json

{

"WebApiPublishUrl": "http://localhost:61330/",
"ConnectionStrings": {
"PayMyRentEntities": "metadata=res:///PMR.csdl|res:///PMR.ssdl|res://*/PMR.msl;provider=System.Data.SqlClient;provider connection string=\u0022data source=ACDSK3;initial catalog=Phnix;user id=hyd3;password=hyd3;integrated security=false;MultipleActiveResultSets=True;App=EntityFramework\u0022"

}
}

Runtime Error

![272464-image.png]1

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,626 questions
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 3,276 Reputation points
    2022-12-21T00:56:23.32+00:00

    "PayMyRentEntities": "metadata=res:///PMR.csdl|res:///PMR.ssdl|res://*/PMR.msl;provider=System.Data.SqlClient;provider connection string=\u0022data source=ACDSK3;initial catalog=Phnix;user id=hyd3;password=hyd3;integrated security=false;MultipleActiveResultSets=True;App=EntityFramework\u0022"

    Are you trying to use the .edmx created from the existing database by using the ADO.NET Entity Data Model wizard of Visual Studio?

    You will not be able to do that on the EF Core.

    I suggest that you create the context class and entity class form the existing database by using the reverse engineering:

    Reverse Engineering
    https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    Scaffold-DbContext
    https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext

    The connection string will be included in the context class. You can copy it to the appsettings.json.

    1 person found this answer helpful.

  2. Remus Tomsa 0 Reputation points
    2024-06-23T16:28:15.7533333+00:00

    Hey, I had the same issue! The solution is to add an "App.config" XML config file in your .net Core 6 application with the connection string, you can copy it from your old Web.config file. Having the connection string in the appsettings.json doesn't help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.