This article describe entity framework code first migration steps in a simple way .Code first migrations commands and how to deployed to the Azure cloud .
3. Objective
Develop without ever having to open a designer or define an XML mapping file
Define your model objects by simply writing “plain old classes” with no base classes
required
Never missed the database changes
Support for version management
Database can change often
Model classes contain logic
Automatically generate database migration script
6. Initial
Convert Exit database
into code first
migration script(initial
script)
Seed Data
Add static data which
need for static tables
Deploy
Confirm code first
migration deploy
method
CI
Continuous
integration with new
Migration scripts
7. Initial Script
Initial Script contain Exit database migration
details
1. Exit database table creation scripts
2. Exit Triggers,Store Procedures create
details include in that table
Application Changes which need for code first
migration
1. Code first migration models
2. Enable code first migration in target
project
8. Main details need to know in code first
migration script
Table Creation model
10. OnModelCreating method in DBContext.cs
- PluralizingTableNameConvention (set the
table name to be a pluralized version of the entity
type name)
modelBuilder.Entity<Item>().ToTable("Item");
- OneToManyCascadeDeleteConvention
(enable cascade delete for any required
relationships)
- modelBuilder.Entity<FlightData>().Property
(o => o.BaseFare).HasPrecision(8, 2);
- modelBuilder.Entity<Log>()
.Property(f => f.XmlData)
12. In second method you can insert large data set
use in csv file inside the seed method
13. Automatic Migration
Migrate your code first model changes into database without codefirst migration script
But in a team environment you need to manually create migration script and then need to migrate
code first model changes(Recommended way and I personally use that way)
More details
https://msdn.microsoft.com/en-us/data/jj554735.aspx
14. Deploy
When application deploy into the cloud you need to do few changes in a web config file
More details http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-
in-code-first.aspx
After successful deploy ,first time execute in DBContext ,code first migration changes migrate into
your target database
17. Can I migrate to early version ?
Yes you can upgrade and downgrade
code first version
18. How can I identify codefirst migration done
or not ?
You can check MigrationHistory table in a target
database
It contain migration history details
Then you can identify what is the last migration
script migrate into the target database
19. Compare Exit database and new database
use in visual studio
Use Visual studio new schema comparison
option to compare database changes
Use in that tool you can compare difference
between exit and new database then you can
verify initial migration done or not
20. Basic commands in code first migration
Enable Migrations
Add-migration
Update-database
Update-database -TargetMigration:”Migration Name”
get-migration
More details
https://dzone.com/articles/ef-migrations-command
21. Drawbacks in live environment
Migrations - Team Environments (Resolve Code First Migration Merge Issues)
https://channel9.msdn.com/Blogs/EF/Migrations-Team-Environments/player
https://coding.abel.nu/2012/02/ef-migrations-and-a-merge-conflict/
22. Code first migration ebooks
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-
entity-framework-data-model-for-an-asp-net-mvc-application