Migration does not work properly
nighttiger1990 opened this issue · comments
Describe the bug
When I use any migrations command, the last pending migration migrate automatically. This cause I can't remove last migrations.
To Reproduce
Steps to reproduce the behavior:
1./ Create new project using dotnet template
dotnet new ca-sln -cf None -o YourProjectName
2./ Modify any entity in domain
public class TodoList : BaseAuditableEntity
{
public string? Title { get; set; }
public Colour Colour { get; set; } = Colour.White;
+ public string? Descriptions { get; set; }
public IList<TodoItem> Items { get; private set; } = new List<TodoItem>();
}
3./ Add next migration without runing application
dotnet ef migrations add Migration1AfterInit -p .\src\Infrastructure\ -s .\src\Web\ -o Data/Migrations
4./ Don't run application then check database 00000000000000_InitialCreate
migrations applied (wrong from here)
5./ Modify something next
public class TodoList : BaseAuditableEntity
{
public string? Title { get; set; }
public Colour Colour { get; set; } = Colour.White;
public string? Descriptions { get; set; }
+ public string? Descriptions2 { get; set; }
public IList<TodoItem> Items { get; private set; } = new List<TodoItem>();
}
6./ Add next migration without runing application
```bash
dotnet ef migrations add Migration2AfterInit -p .\src\Infrastructure\ -s .\src\Web\ -o Data/Migrations
7./ Don't run application then check database *********_Migration1AfterInit
migrations applied (wrong here too)
8./ Remove last migration (wrong here too)
dotnet ef migrations remove -p .\src\Infrastructure\ -s .\src\Web\
got errors
The migration '***********_Migration2AfterInit' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration instead.
Additional context
Comment this code in Program.cs
at Web
Project, everything work perfectly, but we can't seed data
await app.InitialiseDatabaseAsync(); <-- Comment this ef tool will work
and I tried but not work
if (!EF.IsDesignTime)
{
await app.InitialiseDatabaseAsync();
}
Expected behavior
Ef migration feature work properly with seed data.
You could make it conditional by passing an arg when starting the app:
if (app.Environment.IsDevelopment() && args.Contains("--init"))
{
await app.InitialiseDatabaseAsync();
}
Then you should be able to run it using:
dotnet run --init
This will allow for a dev to remove a migration and allow them to choose when they want to initialise the app.