EF application Codefirst mode, basic usage points of data migration notes

Source: Internet
Author: User

The first time to use EntityFramework to do codefirst development, in doing data migration encountered a lot of problems, spent a whole day to learn to adjust, finally learned the basic usage and points. Now after finishing paste out, I hope that the same as my initial users can have some help, less go some detours, less time to explore, are worth.


I. Model DESIGN
1. Follow the EF standard and note the table relationship pairing
2. In the data model, try to write all the required attributes and descriptions
3. EF default ID field primary key, if not, specify primary key

Two. Data migration

1. Command run Environment: Visual Studio toolbar, tools->nuget Package Manager, Package Manager console

2. Basic commands and Common parameters
> Get-help commands to get help (example: Get-help enable-migrations–detailed)
–detailed Detailed Usage
> Enable-migrations Enable Migration
-force forced override
-projectname Target Project (the project where the migration class resides)
-startupprojectname Startup Project (contains the project where the database connection string configuration file resides)
-contexttypename databases (classes) that need to be migrated
-CONNECTIONSTRINGNAME Specifies the name of the connection string used in the configuration file
-connectionstring Specifies the connection string to use
-CONNECTIONPROVIDERNAME Specifies the provider name of the connection string
> add-migration Adding migration Scripts for pending model changes
-force
-projectname
-startupprojectname
-configurationtypename specifying the migration configuration to use
-ignorechanges ignores the detection of a pending model change and creates an initial, empty migration for an existing database-enabled migration.
-connectionstringname
-connectionstring
-connectionprovidername
> update-database to update a pending migration to a database
-force
-projectname
-startprojectname
-configurationtypename
-connectionstringname
-connectionstring
-connectionprovidername
-sourcemigration only works if-script is open. Specifies the name of the migration to use as the starting point for the update. Ignore then use the last applied migration.
-targetmigration Specifies the name of the migration to which the database is updated.
-script Generating SQL scripts
> Get-migrations get migrated that has been applied

3. Examples of migration procedures:

A. Enable migration for the first time, enter the command:

Enable-migrations-projectname Databasemodel-startupprojectname Membercontext-contexttypename DataBaseModel.MemberContext.MemberEntities

And hit enter, then open the Configuration.cs file in the generated Migrations folder,

Automaticmigrationsenabled = False in the construction method, and automaticmigrationsenabled = true;

B. When the model is changed, enter the command: add-migration update20150508-projectname database-startupprojectname member-force and hit Enter to create the migration script

Then enter the command: Update-database-verbose-projectname database-startupprojectname member and hit Enter to perform the migration operation

4. Configure Automatic migration

Register an automatic migration in the application's entry method (function):

Database.setinitializer (New Migratedatabasetolatestversion<ordercontext, configuration> ());
Note: Configuration.cs generates a internal sealed class, which needs to be modified to public if it is not in the startup assembly

5. From the Visual Studio environment to do database version migration, available Migrate.exe, can be consulted: http://msdn.microsoft.com/zh-cn/data/jj618307

Three. Precautions:

1. The connection string is not as complex as the Dbfirst auto-generated, in the following format:

<add name= "memberentities" connectionstring= "server=.;  Database=member;integrated security=true; " Providername= "System.Data.SqlClient"/>

2. When there is a pending model change, it causes the operation to be abnormal, note the operation steps, and, if necessary, clean up the suspended migration.

EF application Codefirst mode, basic usage points of data migration notes

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.