Step-by-Step EF Series "3, Data Migration"

Source: Internet
Author: User

We do not have a lot of content in each article, so we hope that in the process of learning to finally be able to personally tap the code so that more conducive to master.

We now go on to the example of the previous article, we now add a field to the random table Createtime create date run it and see what happens.

Modify the entity class, the code to share with you

 Public Partial classPost {/// <summary>        ///The primary key ID of the essay/// </summary>         Public intPostID {Get;Set; } //the title of the essay         Public stringPosttitle {Get;Set; } /// <summary>        ///Operating Person/// </summary>         Public intBloguserid {Get;Set; } /// <summary>        ///Date Created/// </summary>         PublicDateTime Createtime {Get;Set; } /// <summary>        ///Lazy Load Blog users/// </summary>         Public VirtualBloguser Bloguser {Get;Set; } }

Run a bit.

You will see this error as above. This chapter should have been the 2nd, because if you had tried it yourself, you would have reported the same mistake if you had changed the attribute relationship of the entity.

If you also have this error, congratulations, you are progressing!

You can see the literal meaning, he asked us to use Migrations for database update migration.

Then how to operate it, then we will directly start!!!

First, enable automatic migration

1. Run the command enable-migrations under package Manager Console and you will see the following as you complete it:

Two classes were generated:

What are these two classes for? For the time being, I will not explain what's behind. Here I share a blog about migration interesting to see

Not interested in continue to look down!!

2, EF6 itself has integrated the migrations that we mainly demonstrate is manual to complete the work of the migration, implementation of the application started automatically upgrade

1) I did not use the above method, I was directly created a configuration class

2) We look directly at the code

 Public classConfiguration:dbmigrationsconfiguration<blogdbcontext>    {        /// <summary>        ///Initializes a<see cref= "migrationsconfiguration"/>new instances of type/// </summary>         PublicConfiguration () {//Enable automatic migrationautomaticmigrationsenabled =true; //Gets or sets a value indicating if automatic data loss is acceptable [prudent setting]automaticmigrationdatalossallowed =true; }      }

The core code is two sentences, simple! Does it feel like learning ef so easy! You cannot use so easy because we are talking about the simplest. It is difficult to learn in depth.

3. Then add the code inside the Global.asax to update the database to the newest method on behalf of the application initialization

4. Operation effect

The whole article, the need to write code is not more than 5 lines, so also use a picture to replace, in order to let the study you can try.

Step-by-Step EF Series "3, Data Migration"

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.