Solution of migration Deletion error datasheet in EF

Source: Internet
Author: User
Tags manual

Four orders, we're not going to say anything. The difference between automatic and manual migration:

Change the automaticmigrationsenabled in the Configuration.cs file under migration to True, that is, the automatic migration of migration is turned on, and when the automatic migration is turned on, You enter the command update-database. Sending the program automatically generates a 201403030033_atuomigration.cs migration file that you cannot see from the Migrations folder. To put it bluntly, automatic migration means that the program automatically compares the database, and then converts the changes in the code to the changes in the database.

Manual migration is needless to say, add-migration + naming can be completed, the program will be in the Migrations folder to generate the corresponding scaffolding, with update-database that can complete the migration.

With the previous questions in view, let's look at the problems I've encountered.

Error when running the program:

Models that support the "Equipdbcontext" context have changed since the database was created. Consider using Code-I to migrate the update database (http://go.microsoft.com/fwlink/?LinkId=238269).

This means that the database model has changed, but I have used the migration, why will it change? My database has two tables, one userinfoes, one is equipments, I open the database and found equipments was deleted.

Using automatic migration again prompts that automatic migration is not applied and may result in data loss. Plus-force enforcement, equipments appeared, but Userinfoes was deleted, repeatedly is to delete this, add that Zhang.

Instead of automatic migration, manually, the resulting migration scaffolding, the up and down methods are empty, in other words, the program does not consider it necessary to update the database.

What is the reason for God's horse?

Find our model, my model is in accordance with the online tutorials to write Equipment.cs including the equipment class, but also inherited from the DbContext Equipdbcontext UserInfo.cs includes UserInfo and Userdbcontext inherited from DbContext.

Did you find the problem? Yes, we have two dbcontext, but we all point to the same database. When updating the Equipdbcontext, migration thought Userinfoes's table was superfluous, so it was deleted. The same is true of Userdbcontext.

We found that is the evening's tutorial to the pit, the correct model class is written, to create a Shared.cs file, the DbContext content to a folder, and named the project name +dbcontext:

The code is as follows Copy Code
1
Namespace LQMS. Models
2
{
3
public class Lqmsdbcontext:dbcontext
4
{
5
Public Lqmsdbcontext ()
6
: Base ("DefaultConnection")
7
{
8

9
}
10
Public dbset< Userinfo> Userinfos {get; set;}
11
Public dbset< Equipment> equipments {get; set;}
12
}
13
}

This way we only have a lqmsdbcontext device context, migration can accurately identify the changes!

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.