EF Code First Migration summary, efmigration

Source: Internet
Author: User

EF Code First Migration summary, efmigration
Enable Migration

1. Open the Package Manager Console window through Tools> Nuget Package Manager> Package Manager Console.

2. Default Project: select the Project where the current DbContext is located

3. Enable Migration using the command

Enable-Migrations

After DbContext is enabled, a Migrations folder is generated in the corresponding project of DbContext, which is mainly used to store the update description files automatically generated by each version.

Similar:

Add-migration 20160201003

The names in the following sections are usually composed of the version numbers of the current day by the date of the current day. Another good practice is to name the names based on the added service names. The English names will be better.

Update Library

Database updates can be divided into Manual updates and Automatic Updates. Generally, the Code First Migration article will talk about how to Update the changes to the Database through the Update-Database command, normally, this can be done in the DEV environment because it is directly connected, but our Staging and Production environments cannot be directly connected. What should we do?

As mentioned above, the update can be automatically updated. That is to say, you have generated a migration script through add-migration. When the program is started, if it finds that the interfaces of the database are inconsistent, this requires updates. If the status is automatically updated, the program will be automatically executed (meaning equivalent to update-database, and the results will be the same, but it does not actually verify whether it is also the update-database Command). Therefore, whether it is the dev environment or staging, the production can complete database updates through automatic update-database.

How to Set Automatic Updates

There are two steps:

1. There is a configuration file in the migrations folder. The Code is as follows:

public Configuration()        {            AutomaticMigrationsEnabled = true;            AutomaticMigrationDataLossAllowed = false;        }

After AutomaticMigrationsEnable = true, set EF to automatically update the database.

2. Add the following to Global. cs or any program startup:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<CustomerDbContext, Configuration>());

Set an Initialization Configuration for the current DbContext.

Potential problems

Automatic migration may result in loss of existing data when some of the names or fields are deleted. Note that I have made further research achievements during the exploration process, the description of the blog is updated.

 

[Supplement]

Update the initialization script
Public partial class initdefadefadata: DbMigration {public override void Up () {SQL (@ "-- initialize the original FileType data insert into FileType (SysId, FileTypeId, FileTypeName, Status, CreationTime) select NEWID (), 10001, 'customer information', 0, GETDATE (); insert into FileType (SysId, FileTypeId, FileTypeName, Status, CreationTime) select NEWID (), 10002, 'order information', 0, GETDATE (); insert into FileType (SysId, FileTypeId, FileTypeName, Status, CreationTime) select NEWID (), 10003, 'Vehicle information', 0, GETDATE (); -- initialize the insert into FileCategory (SysId, FileCategoryId, FileCategoryName, Status, CreationTime) select NEWID (), 1, 'image', 0, GETDATE (); insert into FileCategory (SysId, FileCategoryId, FileCategoryName, Status, CreationTime) select NEWID (), 2, 'photocopies ', 0, GETDATE (); insert into FileCategory (SysId, FileCategoryId, fileCategoryName, Status, CreationTime) select NEWID (), 3, 'video', 0, GETDATE (); insert into FileCategory (SysId, FileCategoryId, FileCategoryName, Status, CreationTime) select NEWID (), 4, 'excel file', 0, GETDATE (); insert into FileCategory (SysId, FileCategoryId, FileCategoryName, Status, CreationTime) select NEWID (), 5, 'Word file', 0, GETDATE (); ");} public override void Down () {SQL (@" truncate table FileType "); SQL (@ "truncate table FileCategory ");}}

You can use SQL functions to execute custom SQL scripts.

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.