Entity Framework Code First Migrations
description
Transcript of Entity Framework Code First Migrations
![Page 1: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/1.jpg)
Entity Framework Code First Migrations
By Shahed Chowdhuri
Don’t drown in database design during development
@shahedC
WakeUpAndCode.com
![Page 3: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/3.jpg)
Tech Check
ASP.NETMVC
EntityFramework
EF Code-First
Development
EF Code-First Migrations
Are you familiar with…?
![Page 4: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/4.jpg)
Introduction
ASP.NETMVC
web app
DB
EnterpriseWebApplication
![Page 5: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/5.jpg)
Options and Alternatives
A. Database-
First(EDMX)
B. Model-First
(Visual Designer)
C. Code-First (automatic migrations)
D. Code-First
(manual migrations)
![Page 6: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/6.jpg)
A. Database-First (EDMX)
ASP.NETMVC
app code DB
ADO.NET Entity Data
Model (EDMX)
DB updates EDMX Model
Code uses EDMX
![Page 7: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/7.jpg)
A1. Generate EDMX
![Page 8: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/8.jpg)
A2. Update Model From Database
![Page 9: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/9.jpg)
B. Model-First (Visual Designer)
Source: MSDN
![Page 10: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/10.jpg)
C.Code-First (automatic migrations)ASP.NET
MVCapp code
DB
Migration code
(auto-generated)
auto-updates
uses.NET
models(hand-coded)
generates
![Page 11: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/11.jpg)
D. Code-First (manual migrations)ASP.NET
MVCapp code
DB
Migration code
(auto-generated)
manuallyupdates
uses.NET
models(hand-coded)
generates
![Page 12: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/12.jpg)
What are we trying to solve?
![Page 13: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/13.jpg)
Conflicts Within Organization
Dev Team X
Your Dev
Team
DB Admins
DB Architects
Dev Team Y
![Page 14: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/14.jpg)
Main Agenda
1. Models & Mapping
2. Connection Strings
3. Enable Migrations
4. Update Database
5. Push Code + Migrate Server DB
6. Process Workflow
![Page 15: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/15.jpg)
1. Models & Mapping
![Page 16: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/16.jpg)
2. Connection Strings
For more info:http://wakeupandcode.com/all-your-database-are-belong-to-us/
![Page 17: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/17.jpg)
2a. Connection Strings (explained)
1. How can I point to my own DB?– Each dev has own ConnectionStrings.config
2. How do I avoid checking in to Source Control?– Remove file from .csproj, keep it local
3. How will server deployments work?– Use XML Transforms, i.e. Web.Prod.Config
4. Do I have to include credentials in .config?– No, use machine access to SQL server instead
For more info:http://wakeupandcode.com/all-your-database-are-belong-to-us/
![Page 18: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/18.jpg)
3. Enable Migrations
![Page 19: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/19.jpg)
migration code
4. Update Database
model code
migration code + seed
devDB
(sql)
![Page 20: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/20.jpg)
5. Push Code + Migrate Server DBcode push code
CI
code pull code
migrate.exeServerDB
![Page 21: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/21.jpg)
6. Process Workflow
dev teams DB architects
communication
DB
![Page 22: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/22.jpg)
Code Walkthrough
![Page 23: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/23.jpg)
Making Migrations Work
1. Discuss upcoming changes with teams.
2. Inspect/Update migration code
3. Update your DB before check-in.(use -script flag to inspect SQL first)
4. Run migrate.exe after deployments.(see \packages\EntityFramework.5.0.0\tools\)
DB
![Page 24: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/24.jpg)
Upgrade or Downgrade• Rollback to a specific migration
• Rollback all migrations (version 0)
• Rollback all migrations ($InitialDatabase)
![Page 25: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/25.jpg)
Migration History• Stored in table __MigrationHistory• Metadata for your EF Migrations• 1 new record for every migration• Automatically generated• Used for upgrades & downgrades
![Page 26: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/26.jpg)
Common Issues1. Pushing model changes without adding a
migration.
– Others will see a Pending Migration!
2. Using an existing database
– Use empty Migration if Model added after Table
3. Customizing constraint names
– Manually edit your Migration code
DB
![Page 27: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/27.jpg)
Additional Topics
Relationships Attributes
![Page 28: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/28.jpg)
RelationshipsDBContext.OnModelCreating()
Source: Excella Lean presentationhttps://github.com/excellaco/ExcellaLean
![Page 29: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/29.jpg)
Data-Annotation Attributes
Source: MSDN
![Page 30: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/30.jpg)
Conclusion
Code-First Migrations
Dev Team Synchronize
d
Server Deployment
Database Versioning
Continuous
Development
![Page 32: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/32.jpg)
Troubleshooting the Sample
Trouble with Package Manager commands?• Try the following:
– Delete your Packages folder first– Open .sln in VS2012– Restore NuGet Packages when prompted– Clean Solution– Uninstall EF via NuGet– Reinstall EF via NuGet– Rebuild your solution
![Page 33: Entity Framework Code First Migrations](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816633550346895dd99f3a/html5/thumbnails/33.jpg)
Online Resources• EF Code First Migrations on MSDN:
– http://msdn.microsoft.com/en-US/data/jj591621• Web.config & configSource:
– http://wakeupandcode.com/all-your-database-are-belong-to-us/• PluralSight EF Migrations video tutorial:
– http://pluralsight.com/training/Courses/TableOfContents/efmigrations• My blog post on EF Code First Migrations:
– http://wakeupandcode.com/entity-framework-code-first-migrations/• Code First Data Annotations (Julie Lerman)
– http://msdn.microsoft.com/en-us/data/jj591583.aspx• Data Annotations in the Entity Framework (MSDN)
– http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx
• Troubleshooting– http://stackoverflow.com/questions/10999561/exception-raised-when-im-try
ing-enable-migrations-in-ef-4-3-1