EF 4.3 code-based data migration Drill

Source: Internet
Author: User
      1. First step: CreateMigrationscodedemoConsoleProgram;
      2. Step 2: add the latest versionEntityframeworkNuget package is included in this project:
    • Tools> library Package Manager> Package Manager Console.
    • Run'Install-package entityframework'COMMAND
  • Step 3: Add a blog class and a blogcontext inherited from dbcontext:
     using system; using system. collections. generic; using system. LINQ; using system. text; namespace migrationscodedemo {public class blog {public int blogid {Get; set;} public string name {Get; Set ;}}} 

     using system; using system. collections. generic; using system. LINQ; using system. text; using system. data. entity; namespace migrationscodedemo {public class blogcontext: dbcontext {public dbset 
       
         blogs {Get; Set ;}}
       
  • Step 5: Write down the following in the console Entry Program. CS file:Code:
    Using system; using system. collections. generic; using system. LINQ; using system. text; namespace migrationscodedemo {class program {static void main (string [] ARGs) {using (var db = new blogcontext () {dB. blogs. add (new blog {name = "another blog"}); dB. savechanges (); foreach (VAR blog in dB. blogs) {console. writeline (blog. name );}}}}}

    After the preparation is complete, run the program and you will findMigrationscodedemo. blogcontextThe database will appear in your. \ sqlexpress.

  • After the database is generated, it is impossible to meet all future needs. What if we want to change the existing database? (Ef4.3 the powerful data migration function is displayed)
    • Let's try to add a URL attribute to the existing blog class.
      Using system; using system. collections. generic; using system. LINQ; using system. text; namespace migrationscodedemo {public class blog {public int blogid {Get; set;} public string name {Get; set;} Public String URL {Get; Set ;}}}

      If you run the program again, the system will report an error because the database does not match your model,

      "The model backing the 'blogcontext' context has changed since the database was created. Consider using code first migrations to update the database (Http://go.microsoft.com/fwlink? Linkid = 238269)."

    • According to the error message, useCode first migrationsTo update the database;The first step is to enable migration as our project.
      1. Run the 'Enable-migrations 'command in Package Manager Console

    • After the preceding command is executed, a new file is created in your project. There are two files in the folder:Configuration andInitialcreate Migration

    • Next, we can use EF's migration to add new attributes for our blog and synchronize them to the database,
      1. Run the 'add-migration addblogurl' command in the Package Manager Console. Then, a new addblogurl migration file is created in the project folder.
        Namespace migrationscodedemo. migrations {using system. data. entity. migrations; public partial class addblogurl: dbmigration {public override void up () {AddColumn ("blogs", "url", c => C. string ();} public override void down () {dropcolumn ("blogs", "url ");}}}

        There are mainly two functions in this section, where up is used for data migration and down is used for data backtracking. If you think these operations are not enough, you can manually modify the operations in it,

      2. Finally, run the 'Update-database' command in Package Manager Console, so that our modifications to the model will be updated to the database,

    • After adding the field, we found that the original design is better, then we need to trace back to our database:
      Run the 'Update-database-targetmigration: "initialcreate" 'COMMAND in Package Manager Console. Then, the database returns to the time when it was just created,
    • View SQL: After executing the relevant commands on the Package Manager Console, the database changes accordingly. If we want to see what these commands did for us during data migration, we can use-script to obtain each Migration SQL, for exampleThere are only two migrations foldersMigration

      We want to see which scripts initialcreate executes: we can run the 'Update-database-targetmigration: "initialcreate" 'COMMAND in Package Manager Console,

      To view the specific scripts executed by addblogurl, run the 'Update-database-targetmigration: "addblogurl" 'COMMAND In the Package Manager Console,

      Once these commands are executed, the related. SQL file will be opened in Visual Studio

      Note that code first migrations will run the migration pipeline but instead of actually applying the changes it will write them out to. SQL file for you. this is the original story on msdn. I am worried that my translation will not be accurate,

      Command keyword summary:

        1. Install-package entityframework
        2. Enable-migrations
        3. Add-migration
        4. Update-Database
        5. Update-database-verbose (related scripts are displayed in the Package Manager Console and finally applied to the database)
        6. Update-database-script-sourcemigration: $ initialdatabase-targetmigration: "addblogurl" (where addblogurl is migration name, which is the generated SQL file but instead of actually applying the changes)

       

    After reading. net, developers can embrace Entity Framework (ef4.3 release !!!) I will give you more advice on the learning notes written by msdn.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.