Data migration in Entity Framework 7

Source: Internet
Author: User

(This article also published in my public number "dotnet daily Essence article", Welcome to the right QR code to pay attention to. )

Preface: Although EF7 has redesigned the entity Framework, it is still able to support data migration.

The Entity Framework 7 is a rebirth of the Microsoft ORM Framework, becoming more lightweight. As a result, the data Migration (migration) feature is not turned on by default, and the database created by default does not contain the "__migrationhistory" table. In this case, the changes to the data model require you to manually (via SQL scripts) to modify the corresponding database structure. Of course, you can still enable the automatic data Migration feature.

Mostafa Asaduzzaman shared an article on CodeProject that explains how to migrate data in the entity Framework 7. The approximate steps are as follows:

1, reference Entityframework.commands package in Project.json

   1:"entityframework.commands" "7.0.0-beta4 "

2, add the command "EF" in Project.json:

   1:"Commands": {
   2:        " EF":  "Entityframework.commands"
   3:   },

3. After changing the data model, execute the following command in the project folder to add the new migration code:

   1: dnx. EF Migration Add Newbook

4. Run the Migration code:

   1: dnx. EF migration Apply

In more detail, you can "read the original". As far as my own practical experience is concerned, there are a few points to share with you:

    1. Now the migration command is not run in the Package Manager console before EF6, but runs under the DNX command.
    2. If the DNX command is not valid, then the "DNVM use" may not be executed
    3. If you have not previously enabled migration, it is a good idea to perform a "dnx" before starting the migration. EF Migration Add Initialcreate "for adding a similar initial migration
    4. How do I not want to apply a migration through a command or to automatically apply a migration in code, you can create a method called the Configure method in startup
   1:publicstaticvoid Init (IServiceProvider serviceprovider)
   2: {
   3:     using (var db = serviceprovider.getservice<librarydbcontext> ())
   4:     {
   5:          as Sqlserverdatabase;
   6:         if null)
   7:         {
   8:             Try
   9:             {
  Ten:                 sqldb.applymigrations ();
One   :             }
  :             Catch (Exception ex)
  :             {
  :                 Trace.traceerror (ex. Message);
  :                 throw;
  :             }
  :         }
  :     }
  : }

Original link: http://www.codeproject.com/Tips/988763/Database-Migration-in-Entity-Framework

Data migration in Entity Framework 7

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.