Codefirst database Migration in small memory

Source: Internet
Author: User

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

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.