Wednesday, January 9, 2013

Code First Migration Commands

Code First:
"Whether you have an existing database or not, you can code your own classes and properties that correspond to tables and columns and use them with the Entity Framework without an .edmx file. That's why you sometimes see this approach called code only, although the official name is Code First. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you don't yet have a database, the Entity Framework can automatically create the database for you, or drop and re-create it if the model changes."

Hope you have got the idea of Code First Development approach.

There are two commands which are used too much for the migration and updation of database. I want to share the two commands since I myself use to forget the syntax when I write them in Package Manager in Visual Studio. Although its simple to press Tab Key if you forget someting then Package Manager will show intellisense for you.

Migration Command:
Add-Migration NameOfMigrationFile -StartUpProjectName ProjectName

NameOfMigrationFile - Give name to Migration cs File
ProjectName - Project which contains your Model classes

Update Database Command:
Update-Database -StartUpProjectName ProjectName

ProjectName - Project which contains your Model classes

Revert a Migration Command:
Update-Database -StartUpProjectName ProjectName -TargetMigration NameOfMigrationFile

ProjectName - Project which contains your Model classes
NameOfMigrationFile - Migration file name which needs to be reverted without .cs extension


Hope this helps you in your Code First migrations.

No comments:

Post a Comment