Step by step learn EF Series 3 [data migration], step by step ef

Source: Internet
Author: User

Step by step learn EF Series 3 [data migration], step by step ef

We don't have much content in each article, so we hope that we can finally tap on the code in the course of learning, which is more conducive to understanding.

Now let's continue with the example in the previous article. Now we can add a field CreateTime creation date to the casual table and run it to see what will happen.

Modify the object class and share the Code with you.

Public partial class Post {// <summary> /// the random primary key id /// </summary> public int PostId {get; set ;} // The casual title public string PostTitle {get; set ;}/// <summary> /// operator /// </summary> public int BlogUserId {get; set ;}//< summary> /// creation date /// </summary>
Public DateTime CreateTime {get; set
;} /// <Summary> // delays loading the blog user /// </summary> public virtual BlogUser {get; set ;}}

Run:

Public class Configuration: dbMigrationsConfiguration <BlogDbContext >{/// <summary> // initialize a new <see cref = "MigrationsConfiguration"/> type instance // </summary> public Configuration () {// Enable Automatic migration: AutomaticMigrationsEnabled = true; // obtain or set a value indicating that automatic data loss is acceptable. [set it with caution] AutomaticMigrationDataLossAllowed = true ;}}

The core code is just a few simple words! Do you think learning EF so easy! So easy cannot be used, because what we are talking about here is the simplest. It is difficult to learn more deeply.

3. Add the code in Global. asax to update the database to the latest method during application initialization.

Iv. Running Effect

 

In the entire article, there are no more than five lines of code to be written, so we also use images to replace them so that you can give them a try.

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.