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> ());