Open ejacobg opened 1 month ago
Creating a database before applying your first migration is optional. EF Core will check to see if your database exists before continuing with the migration.
The Update-Database command creates the database and applies the new migration to it.
Create your entities and contexts as usual.
For reasons I don't entirely understand, it's not enough to just run your migration commands against your startup project. eShopOnContainers made use of the IDesignTimeDbContextFactory<T>
to connect to the database. These context factories are also necessary in order to generate the migrations for the IntegrationEventLog.
IntegrationEventLogContext
is in a different project from the Catalog API, but we still want the migrations to be made against the Catalog's database and for the migration files to be written to the Catalog.Api project.Add-Migration -Name InitialCreate -Context IntegrationEventLogContext -OutputDir Infrastructure/IntegrationEventLogMigrations
Use Update-Database
.
If you added a migration but haven't applied it, you can remove it to make further changes.
Remove-Migration
This removes the last migration.
Avoid removing migrations that have already been applied. By deleting the migration file, you remove the option of migrating down, and can break assumptions made by future migrations.
In extreme cases, may be easiest to remove all migrations and start over. This can easily be done by deleting your migrations folder and dropping your database (Drop-Database
). You can then create a new initial migration which will contain your entire current schema.
Any custom migration code will be lost when your migrations folder is deleted. Any custom code needs to be applied again to the new initial migration.
?
SQL Server Express LocalDB
LocalDB is a feature of SQL Server Express (not SQL Server Developer) that allows you to run lightweight SQL Server instances you can connect to.
Check whether you have LocalDB installed:
If not, see here: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb
Create Custom LocalDB Instance
I'll create a custom LocalDB instance for this project rather than using the automatic
MSSQLLocalDB
instance. This helps keep the automatic instance a little cleaner since several databases need to be created.Custom instances should automatically start when you connect to them, and will automatically stop when inactive, but you can always do this yourself:
Connecting to the Custom Instance
The connection string to your custom instance looks something like this:
If you're connecting via SSMS, you can use the following settings: