Sunday, September 23, 2018

Entity Framework Code-First Migration


Purpose: Learn how to use migration history to control your database changes


1.Preparation for Setting Entity Framework, connection string, and relating database


        a. Created a ASP.NET MVC project and install Entity Framework

            Tools -> NuGet Package Manager -> Manage NuGet Packages for Solutions…

              


              

        b. Setup connection string
              
                 project -> right click -> Properties



        c. On "Settings" section, select Connection String on Type field, and edit Value…

           Then you can select the relating database to this project.








2. Create Model and setup DbContext

        a. Create a class which represents your table (Under Models folder).

                 For example, if we want to create a Person table, you can setup it as the following picture.
                 Note: Property name ends with Id will be considered as primary key in table. 

               


        b. Create our DbContext class which inherits DbContext of Entity Framework.
                Note: We use DbContext to coordinate Entity Framework Functionality.

                In addition, add the previous class to DbSet.
                Also, we can specific the database from customized connection string name.
                On this example, we named it “DefaultContext”


       

        
            
     
        c. Then, change the connection string to the name we specified on previous step

            


3. Migration

         
        a. Enable Migration for our project

                 Tools -> NuGet Package Manager -> Package Manager Console



         Run: enable-migrations




        b. Then, we can notice that in our project folder, the Migrations folder was created.





        c. Run: “add-migration init” for creating migration
            Note: "init" is the migration name in this example, you can use anything you want.

            After that, we can see that under the folder, the migration file called “xxxx_init.cs” was created

         

             Then, we can run "update-database" commands to change database.


        d. On SQL Server management, we can find that the Person table was created!
            In addition, this database includes the migration history table (Only One record.)








4. How to revert migrations


        a. Assume that we keep developing our system, and we want to modify person table for more information to meet clients’ requirement.
             For testing purpose in this example, in this step, we want to deliberately make a mistake for the typo issue. And do the Migration to update database!




                       




        b. On SQL Server Management, we can find that the Person table has been changed, and the new migration history has been inserted.









        c. On this moment, we might find that we make a typo mistake.
            
            We can run the following commands on Console to revert to the specific migration history record.
            
            Commands: update-databse -targetMigration init
 

             



        d. Then we can find that the database has been reverted to the first migration we created before. And the second migration history has been removed!






        e. Now, we can delete the wrong migration on our project folder.
            Then, we correct the properties of models (correct the typo). 
            In the end, add the new migration again.

         


        f. Then, we can check the database and migration history.
            Everything is in control now!