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.