如何在 asp.net 6 應用程式中啟動時運行 ef 遷移。
這是我的 Program.cs
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
var serverVersion = new MySqlServerVersion(new Version(8, 0, 23));
builder.Services.AddDbContext<MyContext>(x => x.UseMySql(connectionString, serverVersion)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors());
如何在此處執行 MyContext.Database.Migrate()?
uj5u.com熱心網友回復:
試試下面:
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<MyContext>();
context.Database.Migrate();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364422.html
