1. EFCodeFirst creates a database, creates a console application Portal, and adds EntityFramework through the package manager console. Run the following statement on the package manager console to install EntityFramework. After PMInstall-PackageEntityFramework is successfully installed, the page prompts: in the new Portal console
1. EF Code First creates a database, creates a console application Portal, and adds EntityFramework to the console of the package manager. Run the following statement on the package manager console to install EntityFramework. After the PM Install-Package EntityFramework is successfully installed, the page prompts: in the new Portal console
1. EF Code First creates a database
Create a console application Portal and add EntityFramework in the package manager console.
Run the following statement on the package manager console to install EntityFramework.
PM> Install-Package EntityFramework
After the installation is successful, the following page is displayed:
Add two entity classes to the newly created Portal console application. the code structure is as follows:
The code for the class file PortalContext. cs is as follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.Entity;using System.Data.Entity.Infrastructure;using Portal.Entities;using Portal.Mapping;namespace Portal{ public class PortalContext : DbContext { static PortalContext() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges
()); } public DbSet
Provinces { get; set; } public DbSet
Categories { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new ProvinceMap()); modelBuilder.Configurations.Add(new CategoryMap()); } }}
In the static constructor, when the database model changes, delete the current database and recreate the database.
The database generated after the code is executed:
2. EF Code First database migration
2.1 generate a database
Modify the static constructor of the class file PortalContext. cs to cancel the setting of recreating a new database in the current database when the database model changes.
static PortalContext(){ Database.SetInitializer
(null);}
1> on the package manager console, run the following statement:
PM> Enable-Migrations -EnableAutomaticMigrations
After successful execution, add the Migrations folder in the application code structure of the Portal console and generate the class file Configuration. cs.
namespace Portal.Migrations{ using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration
{ public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(Portal.PortalContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet
.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // } }}
2> in the package manager console, run the following statement:
PM> Add-Migration InitialCreate
After successful execution, add the class file 201309201556388_InitialCreate.cs in the Migrations folder.
3> on the package manager console, run the following statement:
PM> Update-Database -Verbose
The execution result is generated in the same way as the above database.
4> add the City class to the database model, execute the console statement of the package manager, and add the class file 201309201643300_AddCity.cs in the Migrations folder.
PM> Add-Migration AddCity
Execute the console statement of the package manager again
PM> Update-Database -Verbose
Code structure of the Portal console application:
After the database is updated successfully, the City table is added to the database.
2.2 version tracing
Modify the City table in the database and delete the field ProvinceNo. Run the following two statements in the package manager console:
PM> Add-Migration ModifyCity
PM> Update-Database -Verbose
After the execution is successful, the City table structure is changed:
Run the console statement of the package manager to trace the database version.
PM> Update-Database –TargetMigration:"201309201643300_AddCity.cs"
2.3 generate SQL scripts between database versions
Run the console statement of the package manager to generate SQL scripts between database versions. This operation is only used to generate SQL statements and is not executed in the database.
Update-Database -Script -SourceMigration:"201309201643300_AddCity.cs" -TargetMigration:"201309201708043_ModifyCity.cs"
-If TargetMigration is not specified, it is migrated to the latest version by default.
3. other parameters of the EF Code First Migrations statement
1> enable database migration for the specified DbContext
PM> Enable-Migrations -ContextTypeName Portal.PortalContext
2> set whether automatic migration is allowed
Enable-Migrations
Constructor of the generated Configuration. cs file
public Configuration(){ AutomaticMigrationsEnabled = false;}
3> Enable-Migrations specifies the project name
PM> Enable-Migrations -StartUpProjectName Portal
If the default project is selected in the "Package Manager Console", you can not set the "-StartUpProjectName" parameter. if you execute this command multiple times, you can add the-Force parameter.
4> view the executed SQL statement-Verbose command
Update-Database -Verbose
4. download code
Portal.zip
5. References
Http://msdn.microsoft.com/en-US/data/jj591621