Solution for EF Core:
public static void AddMigration()
{
var h1 = new OperationReportHandler(
errorHandler: (x) =>
{
throw new Exception(x);
},
warningHandler: (x) =>
{
Debug.WriteLine($"Warn, {x}");
});
var reporter = new OperationReporter(h1);
Assembly asm = asm; // your Assembly
Assembly startupAsm = asm; // your Assembly
var projName = "<project name here>";
var subPath = $"Database\\_Migrations";
string rootNS = projName;
string subNS = null; // overrides rootNS
var solRoot = "<solution root here>";
var projRoot = Path.Combine(solRoot.FullName, projName);
var outputDir = Path.Combine(projRoot, subPath);
var args = new string[0]; //
var nullable = false; // nullable Ref Types
string language = null; // defaults to "C#"
var sec = ((long)(DateTime.UtcNow - StartDate).TotalSeconds);
var migrationName = $"M_{sec}"; // your Migration Name
MigrationsOperations migrationsOperations = new MigrationsOperations(
reporter,
asm, startupAsm, projRoot, rootNS,
language, nullable, args);
var ctxType = typeof(DbContextCreator).FullName; // your ContextType here
MigrationFiles migrationFiles =
migrationsOperations.AddMigration(migrationName, outputDir, ctxType, snapshotNS);
}
private static DateTime StartDate = new DateTime(2024, 04, 01);
Install the Nuget for Microsoft.EntityFrameworkCore.Design.
Then adjust the .csproj
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets></IncludeAssets>
</PackageReference>
You have to clear IncludeAssets to use the internal types of Microsoft.EntityFrameworkCore.Design.
I tried the code and it generates the same files as Package Manager Console:
Add-Migration -OutputDir Database\_Migrations -Context DbContextCreator
Update Database is just:
ctx.Database.Migrate();