(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:
- Now the migration command is not run in the Package Manager console before EF6, but runs under the DNX command.
- If the DNX command is not valid, then the "DNVM use" may not be executed
- 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
- 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