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 WakeUpAndCode.com

Author: pierce-martin

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

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)

3

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: http://www.dennisdoomen.net/2013/03/entity-framework-56-vs-nhibernate-3.htmlObject-Relational 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:http://wakeupandcode.com/all-your-database-are-belong-to-us/

4a.. Connection StringsConnection StringWeb.config configSourceFor more info: http://wakeupandcode.com/all-your-database-are-belong-to-us/

20

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:http://wakeupandcode.com/all-your-database-are-belong-to-us/4b. 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

DBContext.OnModelCreating()

Source: Excella Lean presentationhttps://github.com/excellaco/ExcellaLeanRelationshipsRelated objectsCustomizing constraint names27Data-Annotation Attributes

Source: MSDNData-Annotation AttributesSource: http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx

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

(diagram with star pattern)

29Downloadshttp://wakeupandcode.com/downloads/

Downloadshttp://wakeupandcode.com/downloads/30

Online ResourcesEntity Framework on MSDNhttp://msdn.microsoft.com/en-us/data/ef.aspxEF Code First Migrations on MSDN: http://msdn.microsoft.com/en-US/data/jj591621Web.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.htmlMy 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.aspxData 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.aspxOnline [email protected]

http://WakeUpAndCode.com Questions?

32