Entity Framework 如何知道要添加哪个迁移?
How does Entity Framework know which migration to add?
我正在尝试在 Entity Framework 的帮助下进行设置,而不必处理 SQL 的代码相关部分。
我创建了一个模型并通过包管理器控制台添加了一个迁移,一切都运行良好它更新并创建了 table。
我想问的是实体怎么知道我要添加哪个迁移
我用过:
add-migration (and put here the name of the migration file)
但我不明白的是它怎么知道我想要 table 的型号?
或者换句话说,如果我在进行任何迁移之前有 2 个模型,我会选择哪个模型?
如果有人能帮助我,我将不胜感激。
提前致谢
您似乎正在使用 entity framework migrations
,但对它的工作原理感到困惑。解释如下:
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.
希望以上解释能对您有所指导,解开您的困惑。你也可以看看 official documents here
我正在尝试在 Entity Framework 的帮助下进行设置,而不必处理 SQL 的代码相关部分。
我创建了一个模型并通过包管理器控制台添加了一个迁移,一切都运行良好它更新并创建了 table。
我想问的是实体怎么知道我要添加哪个迁移
我用过:
add-migration (and put here the name of the migration file)
但我不明白的是它怎么知道我想要 table 的型号?
或者换句话说,如果我在进行任何迁移之前有 2 个模型,我会选择哪个模型?
如果有人能帮助我,我将不胜感激。
提前致谢
您似乎正在使用 entity framework migrations
,但对它的工作原理感到困惑。解释如下:
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 themigrations 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.
希望以上解释能对您有所指导,解开您的困惑。你也可以看看 official documents here