Slide 1

Slide 1 text

Who’s Afraid of Entity Framework? Richie Rump, Dataveloper

Slide 2

Slide 2 text

Who Am I?

Slide 3

Slide 3 text

My Clique

Slide 4

Slide 4 text

Agenda • What is Entity Framework? • How does Entity Framework work? • Why do developers use it? • What should you, the Data Professional, be looking for in an Entity Framework project.

Slide 5

Slide 5 text

What is an ORM? • Object Relational Mapping • Converts pragmatic objects into a relational database. • Hibernate (Java) • Active Record (Ruby) Objects Mapping Data

Slide 6

Slide 6 text

What is Entity Framework • ORM for .NET Applications • Allows the developer to: • Generate databases • Save object data to a database • Generate DDL scripts from object changes • Generate SQL for data retrieval • All done with very minimal code.

Slide 7

Slide 7 text

using (conn = new SqlConnection(connectString)) { conn.Open(); DbCommand cmd = conn.CreateCommand(); cmd.Connection = conn; cmd.CommandText = "SELECT Name, AccountNumber FROM sales.Store"; dbReader = cmd.ExecuteReader(); // do something cmd.Dispose(); conn.Close(); } What Devs Used to Write

Slide 8

Slide 8 text

var stores = context.Stores; Now With EF

Slide 9

Slide 9 text

How to Use EF: Designer

Slide 10

Slide 10 text

How to use EF: Code

Slide 11

Slide 11 text

Table Based Results  Entity Based Results Query Compilation Database Creation How Entity Framework Works Programming Entity Framework: Code First by Julie Lerman and Rowan Miller. Page 9 public Post() { public int Id { get; set; } public int ParentId { get; set; } public string Title { get; set; } public string Body { get; set; } } System.Data.Metadata.Edm EntityType .EdmProperty .AssociationType .EdmType

Slide 12

Slide 12 text

Entity Framework Models Design Centric Code Centric New Database Model First Create .edmx model in designer Generate DB from .edmx Classes auto-generate from .edmx Code First Define classes & mapping in code Database auto-created at runtime Existing Database Database First Reverse engineer .edmx model Classes auto-generate from .edmx Code First Define classes & mapping in code Adapted from Programming Entity Framework: Code First by Julie Lerman and Rowan Miller. Page 3.

Slide 13

Slide 13 text

Entity Framework Versions • v3.5 – .NET 3.5 AKA v1.0 • v4.0 – .NET 4.0. - Lazy Loading, POCO, Perf Enhancements • v4.1 – Code First • v4.2 – Bug Fixes - Semantic versioning • v4.3 – EF Migrations • v5.0 – .NET 4.5 - ENums, Table-valued functions, Spatial Data Types • v6.0 – Async Support, Connection Resiliency • Core v1.0 aka v7.0 – Beta, New Devices (non-Windows), New Data Stores (non-relational)

Slide 14

Slide 14 text

Why Use Entity Framework? • Developer works with objects. • Focus on Business Domain (objects) not DB, Connections, commands, etc. • Developer Productivity • No need to write SQL or CRUD commands

Slide 15

Slide 15 text

Where Does Microsoft Stand • Microsoft is making minimal investment in ADO.NET. • LINQ to SQL is essentially dead • EF is now recommended for Data Access • EF is now open-source software • Next version of Entity Framework is a complete rewrite

Slide 16

Slide 16 text

What Can The Data Professional Do? • Identify common problems the Entity Framework applications have. • Work together with developers to find and address problems. • Educate developers on how SQL Server works.

Slide 17

Slide 17 text

Common Entity Framework Problems • Database Generation • N + 1 Problem • Murder on the Index Express • Searching • Implicit Conversions • Query Complexity

Slide 18

Slide 18 text

Database Generation • EF can generate databases. • EF Migrations can even generate database updates and roll- forward or rollback. • I don’t recommend blindly generating any database from any ORM.

Slide 19

Slide 19 text

Demo Database Generation

Slide 20

Slide 20 text

Database Generation • Things to look for in a ORM generated DB • Normalization • Relationships (They may not exist but should) • Proper Types (NVARCHAR / BIGINT) • Indexes on Foreign Keys • Primary Keys • Alternate Keys • Table Naming Convention (Singular vs. Plural) • Object Naming (such as Keys and Indexes)

Slide 21

Slide 21 text

N + 1 Problem Occurs when object have a 1 : M relationship. Store Customer

Slide 22

Slide 22 text

Demo N + 1 Problem

Slide 23

Slide 23 text

N + 1 Fixes • Use the Includes feature in LINQ queries • Use a Stored Procedure

Slide 24

Slide 24 text

Murder on the Index Express • By default, the a LINQ query will return ALL attributes in an object. • Essentially, it’s performing a SELECT * against the table. • Key Lookup City. • To fix use LINQ projections.

Slide 25

Slide 25 text

Demo Murder on the Index Express

Slide 26

Slide 26 text

Searching • Using the CONTAINS function in a LINQ query creates a LIKE clause. • LIKE has been know to decimate performance. • We can fix this by using a Full Text Search

Slide 27

Slide 27 text

Demo Searching

Slide 28

Slide 28 text

Implicit Conversions • This problem has been mostly fixed in EF 4.1+. • But you can still run into problems with Code First.

Slide 29

Slide 29 text

Demo Implicit Conversions

Slide 30

Slide 30 text

Query Complexity • Complex LINQ queries can generate some awful SQL. • Cut your losses and replace the LINQ query with a SQL one.

Slide 31

Slide 31 text

Demo Query Examples

Slide 32

Slide 32 text

Other Tips • Use a Caching Layer • NoSQL (Redis, Elasticsearch, Solr, etc.) • ASP.NET has a caching library built in. • Use a EF Profiler • Hibernating Rhinos • MiniProfiler

Slide 33

Slide 33 text

Rewind • Entity Framework is used by developers to simplify access to a database. • Entity Framework concepts are a bit tricky and the grammar is important. • Look for the common issues. • Work along side developers to resolve issues.

Slide 34

Slide 34 text

The More You Know • Julie Lerman’s Blog - http://thedatafarm.com/blog • Rowan Miller’s Blog - http://romiller.com • Arthur Vicker’s Blog - http://blog.oneunicorn.com • ADO.NET Blog - http://blogs.msdn.com/b/adonet • #efhelp on twitter • StackOverflow • PluralSite – Julie Lerman’s EF videos

Slide 35

Slide 35 text

Contact Richie Rump @Jorriss http://jorriss.net http://awayfromthekeyboard.com http://dotnetmiami.com http://brentozar.com

Slide 36

Slide 36 text

Thank You!