Be04 introduction to ef 6.0

Preview:

Citation preview

Template designed by

Introduction to EF 6.x

Paolo Pialorsipaolo@pialorsi.comhttp://www.pialorsi.com/blog.aspx

brought to you by

Consulente, project manager e trainer dal 1996

Oltre 40 certificazioni Microsoft conseguiteMicrosoft Certified Solutions Master – SharePoint

Focalizzato su .NET e SharePoint dal 2002

Autore di 10 libri su XML, SOAP, .NET,LINQ e SharePoint

• Microsoft SharePoint 2013 Developer Reference, Microsoft Press

• Build Windows 8 Apps with Microsoft Visual C# andVisual Basic Step by Step, Microsoft Press

• Microsoft SharePoint 2010 Developer Reference, Microsoft Press

Speaker alle principali conferenze IT

Chi sono

Entity Framework 6Let’s start

Official Object Relational Mapper (O/RM) from Microsoft

Included in .NET FrameworkAvailable via NuGet, as well

Available for free on CodePlexSource code available, ‘cause it is Open Source since EF6!

Great option for business-level data access solutionsBut it is an O/RM … don’t expect great performances …

What’s Entity Framework

Developer workflows

NewDatabas

e

ExistingDatabas

e

Designer Centric Code Centric

Database FirstReverse engineer model in EF DesignerClasses auto-generated from model

Model FirstCreate model in EF DesignerGenerate database from modelClasses auto-generated from model

Code FirstDefine classes and mapping in codeEF Power Tools provide reverse engineer

Code FirstDefine classes and mapping in codeDatabase created from codeMigrations apply model changes to database

Code First & EF DesignerAsynchronous query and save

Connection resiliency

Code-based configuration

Database command interception/logging

Performance improvements

Code First OnlyCustom conventions

Insert, update, & delete stored procedures

What’s new in EF6 (1/4)

Open Source!http://entityframework.codeplex.com/

• Source code

• Nightly builds

• Issue tracking

• Feature specs

• Design meeting notes

Only EF team has commit rights

Same license

Same support

Same quality

What’s new in EF6 (2/4)

From the EF teamNested entity types

Improved transaction Support

Multiple contexts per database

DbModelBuilder.HasDefaultSchema

Configurable migrations history table

Creating context with an open connection

Enums, spatial and better performance on .NET 4.0

Default transaction isolation level changed to READ_COMMITTED_SNAPSHOT

What’s new in EF6 (3/4)

From external contributorsCustom migrations operations

Improved warm up time for large models

Pluggable pluralization & singularization service

DbModelBuilder.Configurations.AddFromAssembly

DbSet.AddRange/RemoveRange

What’s new in EF6 (4/4)

Usage scenariosLet’s play with Entity Framework 6

You can create database from code

You can define custom conventions to “understand” your code-based model

Main GoalsOverride default conventions

Centralized code/data conventions

Avoid error prone configurations and repetitive tasks

Define company-level patterns

Code first with custom conventions

When you start from the code …… code changes (often?!)

You can make EF keep track of changesDefining migration paths from one version to another

Code Migrations will generate migration code for you

Code First Migrations

Code First Migrations

Explicit configuration

Data annotations

Custom conventions

Default conventions

Order of Precedence

In case you want to be independent from the database schemaYou can use stored procedures to manage data

Or you can even have already existing databases with stored proceduresFor custom business logic during save actions

If you have to support linked servers

You can now use Code First to map stored procedures to actionsYou can leverage custom conventions, as well

Code First Stored Procedures

Code First

Stored Procedures

One of the smartest news of EF6!

To improve performances and scalabilityTo free-up UI threads

Not to parallelize tasks

Only one async query for each DbContext

Async queries

Async Queries

You can reuse already opened DbConnection instancesFinally!

Exception “Entity Connection can only be constructed with a closed DbConnection” passed away …

Connection ResiliencyAutomatic retry … very useful with Azure SQL Database

Managing Connections

You have always been able to use TransactionScope instances around EF

Now you can also share DbTransactionNot really useful if you have a “clean” architecture

But very useful in any other scenario …

Managed Transactions

Connections

and Transactions

Easy way to see what’s happening in SQL

To see how LINQ is converted to T-SQL

No need of SQL Server Profiler

Useful for debugging

Database Command Logging

Instead of <entityFramework>

Inherit a type from DbConfigurationAutomatic discovery of such a type

Automatic dependency injeciton

Then configure EF by codeFor instance configure IDbCommandInterceptor

Code Based Configuration

Code Based

Configuration

Entity Framework Power Tools (Beta 4)

Allow to design a code first model

Allow to reverse engineer an existing DBProcuding the “Code First” code … “after”

Reverse Engineering Existing DB

Final ThoughtsShall I use EF in real business solutions?

You should consider it …And make a decision based on

• Performances

• Scalability

• Time to market

• Shared knowledge

• Total cost of management

Shall I use EF in real business solutions?

Don’t expect the very best performances you can ever achieve in your lifeYou have to pay the price for the infrastructure

But you have services back from the infrastructure

If you need super-fast solutionsUse custom ADO.NET code

And pay for it

• Implementation, maintenance, tooling, etc.

If you need “classic” performancesGive EF6 a chance …

Decouple code layer and be ready for any changes in the future

Moreover

Grazie a tutti per la partecipazione

Riceverete il link per il download a slide e demo via email nei prossimi giorni

Per contattarmi

paolo@devleap.com

Grazie

Recommended