EF application Codefirst mode, basic usage points of data migration notes
EF6 Code First migration for multiple models
- Get ready
First install the Entity Framework,
Install-package EntityFramework
Then create two of our own context,
MyFirstContext.cs
Using System.data.entity;namespace question.entityframework{public class Myfirstcontext:dbcontext { Public Myfirstcontext (): Base ("Myfirst") { }} }
MySecondContext.cs
Using System.data.entity;namespace question.entityframework{public class Mysecondcontext:dbcontext { Public Mysecondcontext (): Base ("Mysecond") { }} }
- Add Migrations Folder
Enable-migrations-contexttypename question.entityframework.myfirstcontext-migrationsdirectory FirstMigrations
Enable-migrations-contexttypename question.entityframework.mysecondcontext-migrationsdirectory SecondMigrations
ContextTypeName: Databases (classes) that need to be migrated
Migrationsdirectory: Specifies the directory where the files are migrated
- Create a migration record
Create 2 entity classes, MyFirstEntity.cs and MySecondEntity.cs
Then continue to enter in Package Manager console:
Add-migration Initial-configurationtypename Question.entityframework.firstmigrations.configurationadd-migration Initial-configurationtypename Question.EntityFramework.SecondMigrations.Configuration
-configurationtypename: Specifies the migration configuration for different namespaces under different folders that are used by the migration configuration
- Generating data tables
Finally, configure the Web. config input:
Update-database-configurationtypename Question.EntityFramework.FirstMigrations.Configurationupdate- Database-configurationtypename Question.EntityFramework.SecondMigrations.Configuration
Done
EF Codefirst, migrating multiple databases with a project