Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration does not work properly #1062

Open
nighttiger1990 opened this issue Nov 26, 2023 · 4 comments
Open

Migration does not work properly #1062

nighttiger1990 opened this issue Nov 26, 2023 · 4 comments

Comments

@nighttiger1990
Copy link
Contributor

nighttiger1990 commented Nov 26, 2023

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

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.

@ODumpling
Copy link

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.

@nighttiger1990
Copy link
Contributor Author

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.

I think this is not perfect solution when you run app directly from Visual Studio instead of Command Line

@ODumpling
Copy link

You shouldnt need to seed your db every time you run your app.

@nighttiger1990
Copy link
Contributor Author

nighttiger1990 commented May 7, 2024

I found issue and resolved this.
NSwag AfterTargets="PostBuildEvent" make app run one more time beforedotnet ef migrations remove run
Resolved by: Check app run by NSwag ignore InitialiseDatabaseAsync method

if (app.Environment.IsDevelopment())
{
    var lineArgs = Environment.GetCommandLineArgs();
    if(!lineArgs.Any(p => p.Contains("NSwag.AspNetCore.Launcher.dll")))
    {
        await app.InitialiseDatabaseAsync();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants