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.