EF Core has a MigrationBuilder.CreateTable()
method which can be used to manually add tables that don't exist in a DbContext.
EF Core also has .Designer.cs
files for each migration which "Contains information used by EF" and a ...DbContextModelSnapshot.cs
file which are both generated automagically by dotnet ef migrations add
.
When you generate a migration by modifying your DbContext (e.g. adding a new DbSet<SomeClass>
) and running dotnet ef migrations add
the designer and snapshot both include the new table. But when you create an empty migration and then add a call to CreateTable()
with a table name that is not in the DbContext I have found no way of getting the designer and snapshot to include the additional table.
I have not been able to find clear documentation on what EF Core uses the designer and snapshot files for. From experimenting applying and rolling back such migrations work fine without the information in the generated files.
This leaves me with these questions that I can't answer:
You can also run arbitrary raw SQL in migrations which would result in the same questions.
This article on resolving conflicts hints at a way to regenerate the generated files but the ef cli doesn't have a force option.
Snapshot file contains current Model state. It is just serialized your current Model.
Does it matter under any circumstances if tables are in the migration and database but not in designer and snapshot files?
No. EF Core uses snapshot file to generate migrations. It do not connect to database to generate new migrations. It loads snapshot file and compares it with your current model.
designer.cs
How can one regenerated the generated files to include an accurate state (if that's even possible).
It is what scaffold do, generates model from databse, then serilize model to snapshot.
For which purpose designer.cs
files are used for:
So, basically knowing how migrations generation works you can control over it.