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

Describe what this method does? #2288

Closed
bzaar opened this issue Apr 15, 2020 — with docs.microsoft.com · 11 comments
Closed

Describe what this method does? #2288

bzaar opened this issue Apr 15, 2020 — with docs.microsoft.com · 11 comments

Comments

Copy link

bzaar commented Apr 15, 2020

And why does the generated method duplicate the whole database schema on every migration?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ajcvickers
Copy link
Member

@bzaar Please provide some more details on what you are trying to use this method for. It is typically used internally to build the target model for the model-diffing part of Migrations.

@bzaar
Copy link
Author

bzaar commented Apr 16, 2020

I am not using it directly. The method was generated using dotnet ef migrations add.

My main question is why does the generated method duplicate the whole database schema on every migration? I would only expect to see the diff there. My database schema is quite large so I was surprised to see it duplicated in every migration. Or am I doing something wrong here?

@ajcvickers
Copy link
Member

why does the generated method duplicate the whole database schema on every migration

I don't understand what you mean by this. This method creates an EF model. It doesn't directly do anything to the database schema.

@bzaar
Copy link
Author

bzaar commented Apr 16, 2020

Sorry for not making myself clear. Maybe I should step back a bit.

I'm setting up EF Core 3.1.3 migrations against an existing SQL Server database.

This is what I've done so far:

  1. Scaffold my model from the existing database.
  2. Add the InitialCreate migration.
  3. Remove the contents of the Up() method in InitialCreate.
  4. Update database to create the __EFMigrationsHistory table.
  5. Add back the contents of the Up() method.

Then I added a property to an entity class and ran dotnet ef add migration.

Now looking at the BuildTargetModel method of the second migration I see that it contains pretty much the same code as MyDbContextModelSnapshot.BuildModel. I.e. it lists all entities and all their properties and relationships. This seems strange to me as I would only expect to see the differences in migration code.

Is it by design that the whole schema, including tables that have not changed, is duplicated in every migration's BuildTargetModel()?

@smitpatel
Copy link
Member

Migration.BuildTargetModel stores the model which will be created as a result of that particular migration. ModelSnapshot will have same content as last migration's target model. It helps dotnet ef in the case snapshot file is removed.

@ajcvickers
Copy link
Member

@bzaar Yes.

@bzaar
Copy link
Author

bzaar commented Apr 16, 2020

OK thanks for taking the time to clear up my doubts.

@bzaar
Copy link
Author

bzaar commented Apr 16, 2020

Just out of curiosity, under what scenario might BuildTargetModel() for a non-final and non-initial migration be called?

@AndriySvyryd
Copy link
Member

@bzaar It is used to provide supplemental information for some operations, like the column types for InsertDataOperation

@levanlongktmt
Copy link

@AndriySvyryd @ajcvickers from my view I see it's too much code, could I empty this method after migrations has been applied ?

@AndriySvyryd
Copy link
Member

@levanlongktmt If you are not planning to reapply the migrations the recommended approach is to squash them. Currently you'd need to do this manually, but we are working on making this easier.

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

5 participants