我正在使用 .NET 6 和 Entity Framework 6.4.4 (不是 EF Core) 以及使用 MySql Connector.net v8 的 MySql v8 创建一个新的 C# WebApi 项目。
当我尝试启用迁移时,出现以下错误
System.InvalidOperationException:没有命名的连接字符串 “EntitiesContext”可以在应用程序配置文件中找到。
这是一个单一项目的解决方案!我在单独的项目中没有 EntitiesContext。
这是我的 EntitiesContext 类
[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")
//{
//}
}
这是 web.config 文件
<?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>
这是program.cs文件
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();
这是 appsettings.json
{
"ConnectionStrings": {
"EntitiesContext": "server=localhost;database=mytests;user=root;password=123"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
为什么 EF 在 web.config 文件中找不到连接字符串?
这里有一个可行的解决方法,可以避免对连接字符串进行硬编码。
创建一个指定连接字符串的构造函数
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class EntitiesContext : DbContext
{
public virtual DbSet<C_Prod> C_Prod { get; set; }
public EntitiesContext(string connString)
: base(connString)
{
}
}
然后创建接口 IDbContextFactory 的实现,其中对连接字符串进行硬编码。此类在 EF 创建迁移时的设计时使用。您不需要实例化它。 EF 将使用反射自动实例化它。
public class MigrationsContextFactory : IDbContextFactory<EntitiesContext>
{
public EntitiesContext Create()
{
return new EntitiesContext("server=localhost;database=mytests;user=root;password=123");
}
}
现在在program.cs文件中,您可以从appsetings.json获取连接字符串并在EntitiesContext构造函数中使用它
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("EntitiesContext");
builder.Services.AddScoped<EntitiesContext>(_ => new EntitiesContext(connectionString));
这是appsettings.json
{
"ConnectionStrings": {
"EntitiesContext": "server=localhost;database=mytests;user=root;password=123"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
这可能很旧,但我只是在这样一个旧系统上提供一些支持:
而不是:
public EntitiesContext()
: base("name=EntitiesContext")
只需使用
public EntitiesContext()
: base("EntitiesContext")
你应该可以走了。