Using Code-First to Verify Migrations
Use migrations to create & modify the database. Use code-first generation to verify your migrations.
Known as Code-First (or CF), specifying database tables using Plain Old C# Objects (POCOs) is elegant. It provides basic Object Relational Mapping and the classes can be reused in other ways. It is hard to let go of this feature.
But, schemas were made to be broken. So we use Migrations to stay Agile despite database changes.
Unfortunately CF-generation and Migrations do not work well together. Creating tables for the first time seems fine with CF, or if migrations have already been done. But, what if you need to apply migrations after CF creation? The Migrations framework will not know what version the deployed schema is on.
CF ensures that your classes match your database design. Migrations provide no such guarantee. CF is declarative (preferred), while Migrations are procedural.
Therefore, I recommend using CF generation in tests in order to verify migrations.
In a follow-up post I will share some technology & code examples.