'.NET 6 EF 6.4. No connection string named 'xxx' could be found in the application config file
I'm creating a new C# WebApi project using .NET 6 and Entity Framework 6.4.4 (not EF Core) and MySql v8 using MySql Connector.net v8.
When I try to enable the migrations I get the following error
System.InvalidOperationException: No connection string named 'EntitiesContext' could be found in the application config file.
This is a solution with a single project! I don't have the EntitiesContext in a separated project.
Here's my EntitiesContext class
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class EntitiesContext : DbContext
{
public virtual DbSet<C_Prod> C_Prod { get; set; }
// using this constructor the migrations don't work
public EntitiesContext()
: base("name=EntitiesContext")
{
}
// using this constructor the migrations work as expected
//public EntitiesContext()
// : base("server=localhost;database=mytests;user=root;password=123")
//{
//}
}
Here's the web.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.29.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
<connectionStrings>
<add name="EntitiesContext" connectionString="server=localhost;database=mytests;user=root;password=123" />
</connectionStrings>
</configuration>
Here's the program.cs file
using WebApplicationCSharp;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("EntitiesContext");
builder.Services.AddScoped<EntitiesContext>(_ => new EntitiesContext());
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
and here's the appsettings.json
{
"ConnectionStrings": {
"EntitiesContext": "server=localhost;database=mytests;user=root;password=123"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Why EF can't find the connection string in the web.config file?
Solution 1:[1]
Here's a viable workaround to avoid hardcoding the connection string.
Create a constructor where you specify the connection string
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class EntitiesContext : DbContext
{
public virtual DbSet<C_Prod> C_Prod { get; set; }
public EntitiesContext(string connString)
: base(connString)
{
}
}
Then create an implementation of the interface IDbContextFactory where is hardcoded the connection string. This class is used in design time when EF create migrations. You don't need to instantiate it. EF will instantiate it automagically using reflection.
public class MigrationsContextFactory : IDbContextFactory<EntitiesContext>
{
public EntitiesContext Create()
{
return new EntitiesContext("server=localhost;database=mytests;user=root;password=123");
}
}
Now in the program.cs file you can get the connection string from the appsetings.json and use it in the EntitiesContext constructor
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("EntitiesContext");
builder.Services.AddScoped<EntitiesContext>(_ => new EntitiesContext(connectionString));
Here's the appsettings.json
{
"ConnectionStrings": {
"EntitiesContext": "server=localhost;database=mytests;user=root;password=123"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vcRobe |
