Mvc5+ef6+mysql, data migration with Codefirst

Source: Internet
Author: User

I was using mvc4+ef5+mysql to build my own blog. Address: www.seesharply.com; Encountered a problem, is to use the EF Codefirst model to write programs, we generally in the early stages of program development directly in the Global.asax add

System.Data.Entity.Database.SetInitializer (New system.data.entity.dropcreatedatabaseifmodelchanges< Farm.models.farmcontext> ());//Rebuild the database every time the model changes

or Yes,

System.Data.Entity.Database.SetInitializer (New system.data.entity.dropcreatedatabasealways< Farm.models.farmcontext> ());//Rebuild the database every time

In the early development of this is desirable, we can improve their own model classes, and then constantly update the database;

But when we're in the middle of something, and we don't want to update the model every time we do it, and then we have to clean up the data again, EF provides us with the magriation that, in Codefirst mode, when the model changes, the data is migrated, and it has to be said, EF and Microsoft's own SQL Server with the very tacit understanding, here will not repeat, here to a connection is good: http://www.cnblogs.com/libingql/p/3330880.html

But EF and MySQL is not so good, and then EF5 I also eat a lot of bitterness, and then still did not get out, here put out a possible solution connection: http://www.nsilverbullet.net/2012/11/07/6- steps-to-get-entity-framework-5-working-with-mysql-5-5/

If you open the above connection, you will find EF5 configuration MySQL update is actually more trouble, and very prone to error, at least I was so, crying faint in the toilet, accidentally thought not to have EF6, so in EF6 tried, found very simple; just follow the steps below to easily fix it:

We first build the project, write a good model, only for testing:

public class User

{

[Key]

public int Id {get; set;}

public string Name {get; set;}

Public Blog Blog {get; set;}

}

public class Blog

{

[Key]

public int Id {get; set;}

public string Name {get; set;}

public string Content {get; set;}

}

Then add EntityFramework on top of nuget, note that if you are MVC5 project can add controller directly and then select the view with EF, the system will help you add EF6, if it is MVC4, please manually go to nuget Caesar to add EF6

Add complete, then build the controller

Click Add, here will automatically help you to generate some column view operations (but we do not need, the need for the context of childhood), and then the context class to add the following code

OK, then add the MySQL connection string to the config file, the name is the name configured in the entire context class, the default is the Contextt class name

Okay, okay, here's the conecter to add MySQL, so we'll go to nuget and add

Run the program

We'll check the database again.

Next we perform the migration: First add a log class

public class Log

{

public int Id {get; set;}

public string Message {get; set;}

}

At this time if the direct operation will be error, we need to perform the migration;

All right, in the Program management console, enter

Enable-migrations-enableautomaticmigrations
Successful execution will help you add

We'll add it to the context class.

After rebuilding the project

Then add Dbset:public dbset<log> Logs {get; set;}

After entering Add-migration logtable into the program management console, we saw that the program helped us generate this,

In fact, the content is a series of operations on the table

Finally, pour 1 program Management console input Update-database,

After execution, we'll look at the database

All right, success, good luck. (learning, what good advice Welcome message: www.seesharply.com)

Mvc5+ef6+mysql, data migration with Codefirst

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.