EF Code-first Data Migration attempts

Source: Internet
Author: User

Code-first's approach, while eliminating a lot of SQL code, adds to the migration operation. Try the following:

1, the first to search and install NuGet "library Package Manager" in the "Extension Manager", otherwise all commands are not recognized, will report commandnotfound and other errors. If it is offline, download the NuGet.Tools.vsix and install it. This allows the installed EF components to be found automatically in the NuGet package of the management solution, tool-and library package Manager.

2, for the DAL project to install EF, online can choose the default project in NuGet, enter Install-package entityframework; you need to download the EF package manually while offline, After decompression, select the local source directory in NuGet: Install-package entityframework-ignoredependencies-source c:\software\packages, The DAL project has added EF references and added EF information to the project's Packages.config file

3. Then start the migration for this project. Enter Enable-migrations, A new migrations folder will be created in the project containing the restore points for Configuration.cs and initialcreate (this is for SQL Server, and the MySQL word needs to be generated manually by Add-migration itself). If the migration folder already exists, it can be deleted and re-enable-migrations, and the DB and table will be created automatically. It is important to ensure that the DB and model are exactly matched, and EF generates hash values based on the entity graph, and then compares the __migrationhistory data in the DB, and if it does not match, it will error.

4, now the DB has been built, casually change the model, such as the user to add a sex attribute, then how to do the migration? 2 ways: 1) Turn on Automatic Update automaticmigrationsenabled=true;2) of course, for the production environment certainly can not do so, user data is the most important, here add-migration test1. In SQL Server, Ok,mysql will report:

No Migrationsqlgenerator found for provider "MySql.Data.MySqlClient". Use Setsqlgenerator method on the target migrations Configuration class to register additional SQL generators. It is clear that the corresponding SQL generator in the configuration () is the line:

this. Setsqlgenerator ("MySql.Data.MySqlClient"new MySql.Data.Entity.MySqlMigrationSqlGenerator ());

Run Add-migration Test1 again to generate the migrated code.

5, the implementation of Update-database,model changes reflected in the DB, __migrationhistory also added a record. To generate the corresponding SQL script if it is released externally: update-database-script-sourcemigration: $InitialDatabase-targetmigration:test3- Startupprojectname DAL. Ef

6, EF on-line often reported the model backing the context has changed since the database was created. Consider using Code first migrations to update the database.

This problem occurs, the first to confirm that the DB connection string is correct, and then you can export the beta DB and the full version of DB to SQL, and then compare with Ultracompare, to ensure that all columns and properties are consistent. This problem should be avoided as long as the model matches and the data in the __migrationhistory is matched.

EF Code-first Data Migration attempts

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.