Entity Framework Code First
Migrations
December 30, 2015
Diluka Wittahachchige
Audience
Software Engineers
DB Engineers
Implementation Engineers
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
Support
Environment
Entity Framework 4 or higher
.net Framework 4 or higher
Visual studio 2010 or higher
Support Asp.net web forms
and Asp.net MVC
Procedure
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
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
Main details need to know in code first
migration script
Table Creation model
Triggers ,Index and Storeprocedures
migration
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)
Seed Data
Add static data inside your seed method
,specially need for unit test
In second method you can insert large data set
use in csv file inside the seed method
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
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
Deploy to Azure
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-
deployment-with-the-entity-framework-in-an-asp-net-mvc-application
Continuous Integration
Create initial script which base on exit database
Then continue process when new db changes
coming time to time
Can I migrate to early version ?
Yes you can upgrade and downgrade
code first version
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
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
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
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/
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
Demo
https://www.youtube.com/watch?v=Eh7WCkCeWsQ

Entity Framework Code First Migrations

  • 1.
    Entity Framework CodeFirst Migrations December 30, 2015 Diluka Wittahachchige
  • 2.
  • 3.
    Objective Develop without everhaving 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
  • 4.
    Support Environment Entity Framework 4or higher .net Framework 4 or higher Visual studio 2010 or higher Support Asp.net web forms and Asp.net MVC
  • 5.
  • 6.
    Initial Convert Exit database intocode 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 Scriptcontain 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 needto know in code first migration script Table Creation model
  • 9.
    Triggers ,Index andStoreprocedures migration
  • 10.
    OnModelCreating method inDBContext.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)
  • 11.
    Seed Data Add staticdata inside your seed method ,specially need for unit test
  • 12.
    In second methodyou can insert large data set use in csv file inside the seed method
  • 13.
    Automatic Migration Migrate yourcode 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 deployinto 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
  • 15.
  • 16.
    Continuous Integration Create initialscript which base on exit database Then continue process when new db changes coming time to time
  • 17.
    Can I migrateto early version ? Yes you can upgrade and downgrade code first version
  • 18.
    How can Iidentify 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 databaseand 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 incode 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 liveenvironment 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 migrationebooks 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
  • 23.