Introduce a new MigrateAsync that works with asp.net DI
See original GitHub issueMigrateAsync
currently does 2 things
- Find classes that implement IMigration
- Execute the migration logic
We can create a new overload for MigrateAsync
that takes an IEnumerable<IMigration>
as a parameter, and only executes the migration logic
e.g.
public static async Task MigrateAsync(IEnumerable<IMigration> migrationsEnumerable)
{
var lastMigNum = (
await DB.Find<Migration, int>()
.Sort(m => m.Number, Order.Descending)
.Limit(1)
.Project(m => m.Number)
.ExecuteAsync()
.ConfigureAwait(false))
.SingleOrDefault();
var migrations = new SortedDictionary<int, IMigration>();
foreach (var migration in migrationsEnumerable)
{
var t = migration.GetType();
var success = int.TryParse(t.Name.Split('_')[1], out int migNum);
if (!success)
throw new InvalidOperationException("Failed to parse migration number from the class name. Make sure to name the migration classes like: _001_some_migration_name.cs");
if (migNum > lastMigNum)
migrations.Add(migNum, migration);
}
var sw = new Stopwatch();
foreach (var migration in migrations)
{
sw.Start();
await migration.Value.UpgradeAsync().ConfigureAwait(false);
var mig = new Migration
{
Number = migration.Key,
Name = migration.Value.GetType().Name,
TimeTakenSeconds = sw.Elapsed.TotalSeconds
};
await DB.SaveAsync(mig).ConfigureAwait(false);
sw.Stop();
sw.Reset();
}
}
This will allow more control over how to migrate the database
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
How to safely apply an EF Core migrate on ASP.NET ...
There are many ways to migrate a database using EF Core, and one of the most automatic approaches is to call EF Core's...
Read more >Add migration with different assembly
On the command line, it is the project in the current working directory. ... (ASP.NET Core 2+). Had the same issue. Here is...
Read more >Running async tasks on app startup in ASP.NET Core 3.0
In this post I describe how a small change in the ASP.NET Core 3.0 WebHost makes it easier to run asynchronous tasks on...
Read more >Dependency injection in ASP.NET Core
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) ...
Read more >DI : add support to eager load a singleton service · Issue ...
In my example the data access layer cannot contain a IStartupFilter implementation because it is a classlib which has no relation to asp.net....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
oh and when doing the PR, please add test to the tests project pls.
added to v20.18.0-beta1