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

of 32 /32
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during development @shahedC

Author: pierce-martin

Post on 28-Dec-2015




0 download

Embed Size (px)


PowerPoint Presentation

Intro to Entity FrameworkBy Shahed ChowdhuriDont drown in database design during [email protected]

WakeUpAndCode.comEntity Framework Code First Migrations (title page)Dont drown in database design during development

1Welcome, GWU GCC Students!

Sponsors2Tech CheckWeb Application Developmente.g. ASP.NET MVCEntityFramework(ORM from Microsoft)Object Relational Mapping (ORM)Are you familiar with?Are you familiar with:ASP.NET MVC?Entity Framework?Code First development?Code First Migrations?

(slow-reveal diagram + ask the audience)


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

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

4Object-Relational MappingObject-Relational MappingWeb App ORM -- DB5Top ChoicesGood Comparison: MappingWeb App ORM -- DB6Working with EFWorking with EFDatabase-First (EDMX)Model-First (visual designer)Code-First with automatic migrationsCode-First with manual migrations7A. Database-First (EDMX)ASP.NETMVCapp codeDBADO.NET Entity Data Model (EDMX)DB updates EDMX ModelCode uses EDMXA. Options and AlternativesDatabase-First (EDMX)8

A1. Generate EDMX

A1. Options and Alternatives: Database-First (EDMX)Right-click project, click Add -> ADO.NET Entity Data Model, then select existing DB.9

A2. Update Model From Database

A2. Options and Alternatives: Database-First (EDMX)Update your database table(s).Open the EDMX designer, right-click within it, select Update Model from Database.10B. Model-First (Visual Designer)

Source: MSDNB. Options and AlternativesModel-First (visual designer)11C.Code-First (automatic migrations)ASP.NETMVCapp codeDBMigration code(auto-generated)auto-updatesuses.NET models(hand-coded)generatesC. Options and AlternativesCode-First with automatic migrations12D. Code-First (manual migrations)ASP.NETMVCapp codeDBMigration code(auto-generated)manuallyupdatesuses.NET models(hand-coded)generatesD. Options and AlternativesCode-First with manual migrations13What are we trying to solve?

Avoid tug-of-war between :Developers on your teamDevelopers on other teamsDatabase ArchitectsDatabase Administrators14Conflicts Within OrganizationDev Team XYour DevTeamDB AdminsDB ArchitectsDev Team YAvoid tug-of-war between :Developers on your teamDevelopers on other teamsDatabase ArchitectsDatabase Administrators15Main AgendaAgendaAdd models and mapping in codeConnection StringEnable MigrationsUpdate DatabasePush code + Migrate server DBProcess Workflow

161. NuGet Package Manager

NuGet Package ManagerIn Visual Studio, click Tools -> Extension and Updates Search for nuget to download or update NuGet Package Manager172. Entity Framework

3. Entity FrameworkIn Visual Studio, right-click a project (e.g. Dal project)Click Manage NuGet PackagesSearch for entityframeworkAdd EF to your project (or remove it, if already installed)18

3. Models & Mapping

3. Models & MappingCreate a custom DbContext, with a DBSet for each entityCreate a model class for each entityCreate a mapping file for each entity194a. Connection StringsFor more info:

4a.. Connection StringsConnection StringWeb.config configSourceFor more info:


4b. Connection Strings (explained)How can I point to my own DB?Each dev has own ConnectionStrings.configHow do I avoid checking in to Source Control?Remove file from .csproj, keep it localHow will server deployments work?Use XML Transforms, i.e. Web.Prod.ConfigDo I have to include credentials in .config?No, use machine access to SQL server instead

For more info: Connection Strings (explained)How can I point to my own DB?How do I avoid checking in to Source Control?How will server deployments work?Do I have to include credentials in .config?215a. Enable Migrations

5a Enable MigrationsEnable MigrationsConfiguration.csAdd Seed data

22migration code5b. Update Databasemodel codemigration code + seeddevDB(sql)

5b. Update DatabaseUpdate DatabaseRun Seed DataVerify SQLVerify DB

235c. Push Code + Migrate Server DBcode

push codeCIcodepull codemigrate.exeServerDB5c. Push Code + Migrate Server DBPush code to source controlOther developers pull and Update DatabaseMigrate server DB via migrate.exeUse TeamCity to automate this

246. Process Workflowdev teamsDB architectscommunicationDB

6. Process WorkflowProcess WorkflowHandle existing databaseComply with enterprise requirements

25Additional TopicsAdditional TopicsRelationshipsAttributes (Data-Annotation Attributes)26Relationships


Source: Excella Lean presentation objectsCustomizing constraint names27Data-Annotation Attributes

Source: MSDNData-Annotation AttributesSource:

28ConclusionCode First Migrations allows:Continuous developmentSynchronization among team membersDatabase versioningServer deployment

(diagram with star pattern)



Online ResourcesEntity Framework on MSDN Code First Migrations on MSDN: & configSource: vs Entity Framwork: blog post on EF Code First Migrations: First Data Annotations (Julie Lerman) Annotations in the Entity Framework (MSDN) [email protected] Questions?