Migrate data in Entity Framework 7, entityframework

Source: Internet
Author: User

Migrate data in Entity Framework 7, entityframework

(This article is also published in my public account "dotNET daily excellent article". Welcome to the QR code on the right to follow it .)

Question: Although EF7 re-designs the Entity Framework, it still supports data migration.

Entity Framework 7 is a rebirth of the Microsoft ORM Framework and becomes more lightweight. Therefore, the Migration function is not enabled by default, that is, the created database does not contain the "_ MigrationHistory" table by default. In this case, you need to manually (through SQL scripts) modify the corresponding database structure for data model changes. Of course, you can enable the automatic data migration function.

Mostafa Asaduzzaman shared an article on CodeProject, which illustrated how to migrate data in Entity Framework 7. The procedure is as follows:

1. Reference The EntityFramework. Commands package in project. json.

   1: "EntityFramework.Commands": "7.0.0-beta4"

2. Add the "ef" command to project. json:

   1: "commands": {
   2:        "ef":  "EntityFramework.Commands"
   3:   },

3. After changing the data model, run 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

For more details, refer to "read the original article ". As far as my practical experience is concerned, there are several points to share with you:

   1: public static void Init(IServiceProvider serviceProvider)
   2: {
   3:     using (var db = serviceProvider.GetService<LibraryDbContext>())
   4:     {
   5:         var sqlDb = db.Database as SqlServerDatabase;
   6:         if (sqlDb != null)
   7:         {
   8:             try
   9:             {
  10:                 sqlDb.ApplyMigrations();
  11:             }
  12:             catch (Exception ex)
  13:             {
  14:                 Trace.TraceError(ex.Message);
  15:                 throw;
  16:             }
  17:         }
  18:     }
  19: }

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

Related Article

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.