Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during...

34
Entity Framework Code First Migrations By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Transcript of Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during...

Page 1: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Entity Framework Code First Migrations

By Shahed Chowdhuri

Don’t drown in database design during development

@shahedC

WakeUpAndCode.com

Page 2: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Thanks to our Sponsors

Page 3: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Tech Check

ASP.NETMVC

EntityFramework

EF Code-First

Development

EF Code-First Migrations

Are you familiar with…?

Page 4: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Introduction

ASP.NETMVC

web app

DB

EnterpriseWebApplication

Page 5: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

A1. Generate EDMX

Page 8: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

A2. Update Model From Database

Page 9: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

B. Model-First (Visual Designer)

Source: MSDN

Page 10: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

What are we trying to solve?

Page 13: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Conflicts Within Organization

Dev Team X

Your Dev

Team

DB Admins

DB Architects

Dev Team Y

Page 14: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

1. Models & Mapping

Page 16: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

2. Connection Strings

For more info:http://wakeupandcode.com/all-your-database-are-belong-to-us/

Page 17: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

3. Enable Migrations

Page 19: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

migration code

4. Update Database

model code

migration code + seed

devDB

(sql)

Page 20: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

5. Push Code + Migrate Server DBcode push code

CI

code pull code

migrate.exeServer

DB

Page 21: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

6. Process Workflow

dev teams DB architects

communication

DB

Page 22: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Code Walkthrough

Page 23: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Upgrade or Downgrade• Rollback to a specific migration

• Rollback all migrations (version 0)

• Rollback all migrations ($InitialDatabase)

Page 25: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Common Issues

1. 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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Additional Topics

Relationships Attributes

Page 28: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

RelationshipsDBContext.OnModelCreating()

Source: Excella Lean presentation

https://github.com/excellaco/ExcellaLean

Page 29: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Data-Annotation Attributes

Source: MSDN

Page 30: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Conclusion

Code-First Migrations

Dev Team Synchronize

d

Server Deployment

Database Versioning

Continuous

Development

Page 31: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Downloads

http://wakeupandcode.com/downloads/

Page 32: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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 By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

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

Page 34: Entity Framework Code First Migrations By Shahed Chowdhuri Dont drown in database design during development @shahedC WakeUpAndCode.com.

Questions?

@shahedC

http://WakeUpAndCode.com