EF Codefirst Database Migration

Source: Internet
Author: User
Tags visual studio 2010

If a file or assembly fails to load "Microsoft.VisualStudio.Shell, version=10.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" Or one of its dependencies. The system cannot find the file specified.

Reinstall EF Command Install-package entityframework

After this

The main is 3 steps away:

1:enable-migrations-contexttypename MvcMovie.Models.MovieDbContext

2:add-migration Initial

3:update-database

In this section, you will use the Entity Framework code-ahead migration feature to modify the model class and apply the modifications to the database.

By default, when you use Entity Framework code to automatically create a database, as you did earlier in this tutorial, the code first adds a table to the database to help track whether the database schema is synchronous and the model class is generated. If they are not synchronized, the Entity Framework throws an error. This makes it easier to track discovery problems early in development, or you may find cryptic errors at run time.

Establish code-first migration for model modifications
If you are using Visual Studio 2012, in Solution Explorer, double-click the Movies.mdf file to open the database tool. The visual Studio Express for Web will display the Database Explorer, and visual Studio2012 will display the Server Explorer. If you are using Visual Studio 2010, use SQL Server Object Explorer.

In the database tool, right-click on the Moviedbcontext and select Delete.
Go back to Solution Explorer. Right-click on the movies.mdf file and select Delete.

Translator Note: It seems confusing here, in fact, the first step actually performed is to delete the database link, the pop-up deletion confirmation Prompt also illustrates this point, the second step is to really delete the database file in the solution manager.

Build the application and verify that there are no compilation errors.
In the VS2012 Tools menu, click the "library Package Manager"-"Package Manager" console
After the pm> tag in the console, enter "Enable-migrations-contexttypename MvcMovie.Models.MovieDbContext"

Output the following information:
Pm> Enable-migrations-contexttypename MvcMovie.Models.MovieDbContext
Checking whether the target of the context is an existing database ...
The Code first migration has been enabled for Project Mvcmovie.
Pm>

The enable-migrations command above creates a new migrations folder and creates a Configuration.cs file in that directory.
Open the Configuration.cs file by using Visual Studio. Replace the seed method content with the following code:

        protected override void Seed (MvcMovie.Models.MovieDbContext context)        {                       context. Movies.addorupdate (              m = m.name,              new Movie {name = "The Fantasy Rafting trip for Youth", Date=datetime.now},              new movie {name = "one 942 ", date = DateTime.Now},              new Movie {Name =" Feast of the Kings ", date = DateTime.Now}            );                   

The above code requires a class to import namespaces using Mvcmovie.models;

The code-First migration mechanism calls the seed method after each migration, and if there is row data, the method updates the existing data and if it does not, the method inserts the data.

Press Ctrl+shift+b to build the project (if this build operation is not performed here, subsequent steps will fail)

The next step is to create the Dbmigration class to initialize the migration. The migration creates a new database, which is why you deleted the database files in the previous steps.
In the Package Manager Console window, enter the command "Add-migration Initial" to create the initial migration. Where initial can be any name used to identify the initial file that was created. The console output is as follows:

Pm> add-migration Initial
Scaffolding is being built for migrating "Initial".
The designer code for this migration file contains a snapshot of the current Code first model. This snapshot is used to calculate changes to the model the next time the migration scaffolding is built. If you make other changes to the model that you want to include in this migration, you can re-build the scaffolding by running Add-migration 201212060747227_initial again.
Pm>

Code-First migration mechanism creates another class file under the Migrations folder, with the file name Timestamp + underscore + Initial.cs, such as 201212060747227_initial.cs, which contains the code to create the database schema. Migration file names are preset as timestamps to help sort. View the file, which contains a description of the Create Movie library table. When you update the database, the class is executed and the database schema is created. The seed method is then executed and the test data is added.

In the Package Manager Console window, type the "update-database" command to create the database and execute the seed method.
Pm> Update-database
Specify the "-verbose" tag to view the SQL statements applied to the target database.
Applying code-based migration: [201212060747227_initial].
Applying code-based migration: 201212060747227_initial.
The Seed method is running.
Pm>


If you encounter an error that already exists in the table and cannot be created, it is likely that after you have deleted the database, you run the application (recompiling the program and automatically created the database) before executing update-database. In this case, delete the database file again and execute the update-database command. If you still get the error, delete the migrations directory and contents and start the tutorial again.

Run the application and navigate to the/movies address. The seed data shows up.

Note: The seed data here actually refers to some of the test data added after the creation of the library table or system initialization data, such as system parameters, Department root directory, etc.

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.