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