Mysql how the Entity Framework database is migrated and how to better support entityframework.extended

Source: Internet
Author: User

Problem

1. When using entityframework to access MySQL, you will encounter some problems when using the migration to build the database or update the database.

2.entityframework.extended support for MySQL is not very complete, where modifications are not directly used need to do some processing

Solution Solutions

1. First to solve the first problem

Readiness: Download Mysql.Data.Entity with NuGet (dependencies can be downloaded together)

We built the entity in the form of code first. Then create your own DbContext class.

This direct use command

Enable-migrations-projectname Medicalinsurance.domain

Update-database-projectname Medicalinsurance.domain

To generate a migration file

Modify the configuration code as follows:

 Public Configuration ()        {            true;             true ;        }

Use the following command to migrate the build database (be sure to select the project where DbContext is executing the command)

Add-migration Init

Update-database-projectname Medicalinsurance.domain

Find a problem problem

Modify the DbContext file

Features added to the DbContext

In the execution add-migration Init

This time has been successfully executed and the corresponding migration file has been generated

Then perform the Update-database-projectname medicalinsurance.domain to update the migration to the database

If you want to modify the field of an entity, or increase the field of an entity, or add an entity, after the modification is complete

Execute again

Add-migration UpdateDB

Update-database-projectname Alien.ClinicSystem.Data

This allows you to synchronize updates to the database. (No detailed steps will be done here.)

2. Now to solve the second problem

Prepare condition: Download entityframework.extended with NuGet

For specific use of entityframework.extended, please search by yourself. Here I'll just explain the problems and workarounds that are encountered with update.

Let's start with the update operation to see what's wrong. Here I randomly built a controller to test the update (because this project is an MVC project).

I manually added the data to the database:

Now let's modify this data (change the user phone number 1 to "88888888")

Perform a look at the effect:

The above error probably means that the generated SQL statement is incorrect.

How to solve it.

Use the following methods to solve the problem perfectly.

First, modify the code in the DbContext class (red callout):

[Dbconfigurationtype (typeof(MySql.Data.Entity.MySqlEFConfiguration))] Public classClinicsystemcontext:dbcontext { PublicClinicsystemcontext ():Base("MySQLdb") { Database.setinitializer <ClinicSystemContext> (Null); }         PublicDbset<user> Users {Set;Get; }  PublicDbset<charge> Charges {Set;Get; }  PublicDbset<chargeitem> Chargeitems {Set;Get; }  PublicDbset<clinic> Clinics {Set;Get; }  PublicDbset<diagnos> Diagnoses {Set;Get; }  PublicDbset<disease> Diseases {Set;Get; }  PublicDbset<drug> Drugs {Set;Get; }  PublicDbset<materialinfo> Materialinfos {Set;Get; }  PublicDbset<recipe> Recipes {Set;Get; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) { Modelbuilder.hasdefaultschema ( ""); Add comments to the configuration migration remove annotations when running            Base.        Onmodelcreating (ModelBuilder); }    }

Then add the following code to the entry at the start of the project (I am the MVC project so add in Application_Start):

At this point again to perform to see if can be modified.

No error. See if the database content has been modified.

The contents of the discovery database have been modified

Attention:

When using EF. extened, when changing the entity, need to migrate the database, need to want to dbcontext inside the

       Modelbuilder.hasdefaultschema ("");    // add comments to the configuration migration remove  annotations when running

Comments. After the migration is complete, remove the comment.

Description

This is the problem I encountered in the project, as well as the solution, welcome to combat criticism.

Mysql how the Entity Framework database is migrated and how to better support entityframework.extended

Related Article

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.