EF CodeFirst Dome learning, codefirstdome

Source: Internet
Author: User

EF CodeFirst Dome learning, codefirstdome

Create the leledome console application

Install EntityFramework from NuGet Package Manager

 

Create the DbContextDome class and inherit the DbContext

Public class DbContextClass: DbContext {// <summary> // ConnectionString is the database link string set in config /// </summary> public DbContextClass (): base ("ConnectionString") {} public DbSet <user> user {get; set;} public DbSet <BlogModel> blog {get; set ;}}

Set config

<connectionStrings>    <add name="ConnectionString" connectionString="server=.;database=EFCodeFirstDB;uid=***;pwd=***" providerName="System.Data.SqlClient"/>  </connectionStrings>

Create entity model

 public class user    {        public int id { get; set; }        public string username { get; set; }        public string pwd { get; set; }        [Column(TypeName = "nvarchar")]        [MaxLength(200)]        public string Email { get; set; }        public string pwdd { get; set; }    } public class BlogModel    {        public int id { get; set; }        /// <summary>        ///         /// </summary>        public string Tiele { get; set; }        /// <summary>        ///         /// </summary>        public string TieleUrl { get; set; }        /// <summary>        ///         /// </summary>        public string Content { get; set; }        /// <summary>        ///         /// </summary>        public string ImgUrl { get; set; }        /// <summary>        ///         /// </summary>        public DateTime CreateDate { get; set; }        /// <summary>        ///         /// </summary>        public string ReadNumber { get; set; }    }

Run Enable-Migrations on the Package Manager Console to create Configuration ef

 

Finally, execute Update-Database-Verbose to Update the Database.

 

Add the codefirst command

I. Enable-Migrations

  Note:Enable migration for the project. This command adds the Migrations folder for the project, including two files:

Configuration class: This class allows context-based Configuration migration.

InitialCreate migration: this migration is generated before migration is enabled. If the database is not generated before Migration is enabled, the file is not added to the project, but is generated when the "Add-Migration" command is called for the first time.

  Syntax: Enable-Migrations

 

2. Add-Migration

  Note:Based on the changes made to the model since the previous migration, a base frame is built for the next migration.

  Syntax: Add-Migration "[Name]"

Name: Migration display Name, which will constitute the generated migration file Name with the current generated file Timestamp

 

Iii. Update-DataBase

  Note:Migrate all pending applications and databases

  Syntax: Update-Database-[Option]

Option: Vcrbose displays the SQL statement executed by the migration on the console

TargetMigration: [Name] migration to a specific version. If you need to roll back to an empty database, you can write [Name] as $ InitialDatabase

Script is a Script generated for migration without execution.

SourceMigration: [Name] source migration

 

Iv. Automatic migration

  Note:When deploying an application, you want to automatically upgrade the database through a suspended migration when the application starts.

  Code: Database. SetInitializer (new MigrateDatabaseToLatestVersion <DbContext, Configuration> ());

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.