Data Migration in mvc3 EF code first Mode

Source: Internet
Author: User

During codefirst mode development, the entity class and database fields do not match.

For exampleProgramThe product class is defined in

 
Public class product {[hiddeninput (displayvalue = false)] public int productid {Get; set;} [required (errormessage = "Please enter a product name")] public string name {Get; set;} [required (errormessage = "Please Entere a description")] [datatype (datatype. multilinetext)] Public String description {Get; set;} [required] [range (0.01, double. maxvalue, errormessage = "Please Entere a positive price")] public decimal price {Get; set;} [required (errormessage = "Please sepcify a category")] public String category {Get; set;} public byte [] imagedata {Get; set;} [hiddeninput (displayvalue = false)] Public String imagetype {Get; set ;}}

Entityframework will automatically create a database and generate the corresponding table when we run the website for the first time. In this example, sqlserver 2012 and mysql5.5 are used to detect problems during the test. The new mysql. Net connnector version is required to be tested again.

The tables in the database are as follows:

 

Add a new property in the product class, testor

Public class product {[hiddeninput (displayvalue = false)] public int productid {Get; set;} [required (errormessage = "Please enter a product name")] public string name {Get; set;} [required (errormessage = "Please Entere a description")] [datatype (datatype. multilinetext)] Public String description {Get; set;} [required] [range (0.01, double. maxvalue, errormessage = "Please Entere a positive price")] public decimal price {Get; set;} [required (errormessage = "Please sepcify a category")] public String category {Get; set;} public byte [] imagedata {Get; set;} [hiddeninput (displayvalue = false)] Public String imagetype {Get; set ;}Public String testor {Get; set ;}}

Run, error:

The model backing the 'ef dbcontext' context has changed since the database was created. Consider using code first migrations to update the database

It means that the data context changes are inconsistent with the database in codefirst mode and the database needs to be updated.

Solution:

Open the Package Manager Console and enter the following command:

1. Enable-migrations: After the execution is complete, you will find that there are multiple migration folders in the project solution, and there will be a CS file of configuration below.

2. Add-migration XXX: xxx can be customized.

3. Update-database-verbose:-verbose is an optional parameter. When it is attached, the running status is displayed.

 

After each change, you need to repeat steps 2nd and 3.

PS: You can set automaticmigrationsenabled to true in the configuration. CS file, so that after each class change, you only need to enter the command in step 3.

 

Pss: When you delete the product command, you have encountered the problem of automatic migration was not applied because it wocould result in data loss,

Add a-force after Update-database-verbose. However, not paying attention to this will cause data loss. Be sure to pay attention to this.

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.