intro to entity framework by shahed chowdhuri don’t drown in database design during development...

32
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Upload: pierce-martin

Post on 28-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Intro to Entity Framework

By Shahed Chowdhuri

Don’t drown in database design during development

@shahedC

WakeUpAndCode.com

Page 2: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Welcome, GWU GCC Students!

Page 3: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Tech Check

Web Application

Developmente.g. ASP.NET

MVC

EntityFramework(ORM from Microsoft)

Object Relational Mapping

(ORM)

Are you familiar with…?

Page 4: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

• Object-Oriented Programming (e.g. C#)• Visual IDE (e.g. Visual Studio)• Software Frameworks (e.g. Microsoft .NET)• Relational Databases (e.g. SQL Server)

You Should Know…

Page 5: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Object-Relational Mapping

Web App

ORM

DB

Page 6: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Top Choices

NHibernate

• Mature, popular

Entity Framework

• Now mature, integrated

Good Comparison: http://www.dennisdoomen.net/2013/03/entity-framework-56-vs-nhibernate-3.html

Page 7: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Working with EF

A. Database-

First(EDMX)

B. Model-First

(Visual Designer)

C. Code-First (automatic migrations)

D. Code-First

(manual migrations)

Page 8: Intro to Entity Framework By Shahed Chowdhuri Don’t 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 9: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

A1. Generate EDMX

Page 10: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

A2. Update Model From Database

Page 11: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

B. Model-First (Visual Designer)

Source: MSDN

Page 12: Intro to Entity Framework By Shahed Chowdhuri Don’t 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 13: Intro to Entity Framework By Shahed Chowdhuri Don’t 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 14: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

What are we trying to solve?

Page 15: Intro to Entity Framework By Shahed Chowdhuri Don’t 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 16: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Main Agenda

1. NuGet

2. Entity Framework

3. Models & Mapping

4. Connection Strings

5. EF Migrations

6. Process Workflow

Page 17: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

1. NuGet Package Manager

Page 18: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

2. Entity Framework

Page 19: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

3. Models & Mapping

Page 20: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

4a. Connection Strings

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

Page 21: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

4b. 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 22: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

5a. Enable Migrations

Page 23: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

migration code

5b. Update Database

model code

migration code + seed

devDB

(sql)

Page 24: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

5c. Push Code + Migrate Server DBcode push code

CI

code pull code

migrate.exeServer

DB

Page 25: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

6. Process Workflow

dev teams DB architects

communication

DB

Page 26: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Additional Topics

Relationships Attributes

Page 27: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

RelationshipsDBContext.OnModelCreating()

Source: Excella Lean presentation

https://github.com/excellaco/ExcellaLean

Page 28: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Data-Annotation Attributes

Source: MSDN

Page 29: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Conclusion

Entity Framework &

Code-First Migrations

Dev Team Synchronize

d

Server Deployment

Database Versioning

Continuous

Development

Page 30: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Downloads

http://wakeupandcode.com/downloads/

Page 31: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Online Resources• Entity Framework on MSDN

– http://msdn.microsoft.com/en-us/data/ef.aspx• 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/• Nhibernate vs Entity Framwork:

– http://www.dennisdoomen.net/2013/03/entity-framework-56-vs-nhibernate-3.html

• 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

Page 32: Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC WakeUpAndCode.com

Questions?

@shahedC

http://WakeUpAndCode.com