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

How to make a database able to use migrations, in code, after it was created using EnsureCreate #26870

Closed
dgxhubbard opened this issue Dec 1, 2021 · 6 comments

Comments

@dgxhubbard
Copy link

dgxhubbard commented Dec 1, 2021

We are using EF Core 5.

We have a databases for a product that were created using EnsureCreated. We have added migrations to the
product. We need to migrate in code, so we use the Migrate, which is great. I did the migrate and nothing happened. Come to find our databases created using EnsureCreated cannot be migrated. How can we update databases in the field, in code, so that we can call Migrate?

@ajcvickers
Copy link
Member

@dgxhubbard This is essentially the same as having an existing database that you then want to start using migrations with. We are tracking #2167 to make this better, and also dotnet/EntityFramework.Docs#91 to document it.

The general approach for this is to create an initial migration, and then to edit that migration and remove all the code from the Up method. (You can optionally also remove the code from the Down method.) For example:

public partial class StartingPoint : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}

When this migration is applied, it won't change the database (since the database is already in the correct state anyway) but it will create the __EFMigrationsHistory table. Also, the model snapshot in code is in the correct state to track new changes. This means additional migrations can then be added in the normal way.

@sj-net
Copy link

sj-net commented Dec 11, 2021

@ajcvickers this helped me. thanks for this tip One quick question. Should we remove the content of the BuildTargetModel as well in the partial class same like Up and Down?

@ajcvickers
Copy link
Member

@santosh-jallapuram Not sure; @bricelam?

@dgxhubbard
Copy link
Author

@ajcvickers
I am not looking for "having an existing database that you then want to start using migrations with". What I am looking for is generating and applying migrations using a namespace and assembly. I finally understand the multiple provider case in a single assembly. The user has to generate and apply migrations using a separate context for each provider. What I would like as a feature is the user specifies a connection string to generate migrations, and connection string, namespace and assembly to apply a migration. This would remove DbContext from migrations and I feel simplify generating and applying migrations.

@dgxhubbard
Copy link
Author

@ajcvickers
It would be great if migrations could start being used with databases created from EnsureCreated.

@bricelam
Copy link
Contributor

bricelam commented Jan 4, 2022

Should we remove the content of the BuildTargetModel as well in the partial class same like Up and Down?

That code will only be used if you try to dotnet ef migrations remove the second migration, so yeah, probably safe to delete.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants