Be04 introduction to ef 6.0

30
Template designed by Introduction to EF 6.x Paolo Pialorsi [email protected] http://www.pialorsi.com/blog.aspx

Transcript of Be04 introduction to ef 6.0

Page 1: Be04   introduction to ef 6.0

Template designed by

Introduction to EF 6.x

Paolo [email protected]://www.pialorsi.com/blog.aspx

Page 2: Be04   introduction to ef 6.0

brought to you by

Page 3: Be04   introduction to ef 6.0

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

Page 4: Be04   introduction to ef 6.0

Entity Framework 6Let’s start

Page 5: Be04   introduction to ef 6.0

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

Page 6: Be04   introduction to ef 6.0

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

Page 7: Be04   introduction to ef 6.0

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)

Page 8: Be04   introduction to ef 6.0

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)

Page 9: Be04   introduction to ef 6.0

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)

Page 10: Be04   introduction to ef 6.0

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)

Page 11: Be04   introduction to ef 6.0

Usage scenariosLet’s play with Entity Framework 6

Page 12: Be04   introduction to ef 6.0

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

Page 13: Be04   introduction to ef 6.0

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

Page 14: Be04   introduction to ef 6.0

Code First Migrations

Page 15: Be04   introduction to ef 6.0

Explicit configuration

Data annotations

Custom conventions

Default conventions

Order of Precedence

Page 16: Be04   introduction to ef 6.0

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

Page 17: Be04   introduction to ef 6.0

Code First

Stored Procedures

Page 18: Be04   introduction to ef 6.0

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

Page 19: Be04   introduction to ef 6.0

Async Queries

Page 20: Be04   introduction to ef 6.0

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

Page 21: Be04   introduction to ef 6.0

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

Page 22: Be04   introduction to ef 6.0

Connections

and Transactions

Page 23: Be04   introduction to ef 6.0

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

Page 24: Be04   introduction to ef 6.0

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

Page 25: Be04   introduction to ef 6.0

Code Based

Configuration

Page 26: Be04   introduction to ef 6.0

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

Page 27: Be04   introduction to ef 6.0

Final ThoughtsShall I use EF in real business solutions?

Page 28: Be04   introduction to ef 6.0

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?

Page 29: Be04   introduction to ef 6.0

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

Page 30: Be04   introduction to ef 6.0

Grazie a tutti per la partecipazione

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

Per contattarmi

[email protected]

Grazie