To open the Package Manager Console menu item
First, enable-migrations-contexttypename code_first_ data migration. Models.t_dbcontext
Prompt after success: Migration is enabled in the project Code First Data migration. To overwrite an existing migration configuration, use the-force parameter.
and create a new migrations folder and configuration file in the project
Second, modify the seed method in the configuration file and recompile the solution
protected override void Seed (Code_first_ data migration. Models.t_dbcontext context)//contexts that need to be migrated
{
Context. Students.addorupdate
(
i = I.name,
New Student () {name= "small", age=15},//Add test data
New Student () {name= "big", age=17}
);
}
Iii. add-migration Initial//Create an initial migration note: Initial is a random name that is used to name the created migration file.
After success, the {Datestamp}_initial.cs file is created, which contains instructions for creating a students table for the database codefirsesjqy
Iv. Update-database
Execute the Update Database command, the {Datestamp}_initial.cs file will run and create the database structure, and then the execution Seed method will insert the test data into the database.
After success: Specify the "-verbose" tag to view the SQL statements applied to the target database.
Applying code-based migration: [201510190651129_initial].
Applying code-based migration: 201510190651129_initial.
The Seed method is running.
Problem:
1. The context type "code_first_ data Migration" is not found in assembly "TESTMVC". Models.t_dbcontext ".
FIX: Change the default project to a project that you now need to migrate
2. Target Context "code_first_ data migration. Models.t_dbcontext "cannot be constructed. Add a default constructor or provide an implementation of idbcontextfactory.
Workaround: Add a default constructor in the T_dbcontext class public T_dbcontext () {}
Codefirst database Migration in small memory