Home > OS >  How does Entity Framework know which migration to add?
How does Entity Framework know which migration to add?

Time:12-22

I am trying with the help of Entity Framework to set up this without having to deal with the code-related part of SQL.

I created a model and added a migration via package manager console and it all worked well it updated and created the table.

The thing I want to ask is how does the entity know which migration I want to add.

I used:

add-migration (and put here the name of the migration file)

But the thing I don't understand is how does it know which model I want for my table?

Or put it in other words if I would have 2 models before I did any migrations which model would get chosen?

Would really appreciate it if someone could help me out.

Thanks in advance

CodePudding user response:

Seems you are using entity framework migrations and got confused how it works. Here is the explanations:

Question: But the thing I don't understand how does it know which model I want for my table?

  • If you look into your project folder there is the directory Migrations. Inside it all the migrations history logs written into.When we made any changes on data model, EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be
    tracked in your project's source control like any other source file.
  • Once a new migration has been generated, it can be applied to a database in various ways. EF Core records all applied migrations in a special history table, allowing it to know which migrations have been applied and which haven't

Question: If I would have 2 models before I did any migrations which model would get chosen?

  • As said earlier, as it keep track previous migrations history, so in your old model it compares the differences and overrite latest changes that were not written on older files. This is how it works.

Hope above explanations guided you accordingly and redeem your confusions. You can also have a look on official documents here

  • Related