Transcript
Page 1: Entity Framework For DBAs

Entity Framework: Or How I Learned to Stop Worrying and

Learned to Love DevelopersRichie Rump

Jorriss LLC@Jorriss

www.jorriss.net

Page 2: Entity Framework For DBAs

Who is this dude?

Page 3: Entity Framework For DBAs

What We’ll Cover

• What is Entity Framework?• How does Entity Framework work?• Why do developers like it?• What should you, the DBA, be looking for

in an Entity Framework project.

Page 4: Entity Framework For DBAs

2008

Photo Credit: http://www.patrickfallonphoto.com/2008/11/04/election-2008-barack-obamas-election-night-grant-park/

Page 5: Entity Framework For DBAs

2008

Photo Credit: http://www.sacbee.com/static/weblogs/photos/2008/08/014666.html

Page 6: Entity Framework For DBAs

2008

Photo Credit: http://triggerpit.com/2010/11/22/incredible-pics-nasa-astronaut-wheelock/

Page 7: Entity Framework For DBAs

2008

Photo Credit: http://www.nydailynews.com/sports/football/giants/eli-manning-making-quarterback-article-1.1013353

Page 8: Entity Framework For DBAs

2008

Photo Credit: http://www.businessinsider.com/how-burger-king-went-from-mcdonalds-greatest-rival-to-total-train-wreck-2012-4

Page 9: Entity Framework For DBAs

What is an ORM

• Object Relational Mapping• Converts pragmatic objects into a

relational database.– Hibernate (Java)– Active Record (Ruby)

Objects Mapping Data

Page 10: Entity Framework For DBAs

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.

Page 11: Entity Framework For DBAs

How to use EF: Designer

Page 12: Entity Framework For DBAs

How to use EF: Code

Page 13: Entity Framework For DBAs

What Devs Used To Writeusing (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();}

Page 14: Entity Framework For DBAs

Now With EF

var stores = context.Stores.Include(c => c.Customers);

Page 15: Entity Framework For DBAs

So how does this thing work?

Lovingly stolen from Programming Entity Framework: Code First by Julie Lerman and Rowan Miller page 9.

Page 16: Entity Framework For DBAs

Entity Framework ModelsDesign Centric Code Centric

New Database

Existing Database

Model FirstCreate .edmx model in designerGenerate DB from .edmxClasses auto-generate from .edmx

Database FirstReverse engineer .edmx modelClasses auto-generate from .edmx

Code FirstDefine classes & mapping in codeDatabase auto-created at runtime

Code FirstDefine classes & mapping in code

Adapted from Programming Entity Framework: Code First by Julie Learman and Rowan Miller page 3.

Page 17: Entity Framework For DBAs

Versions of Entity Framework

• v1.0 - .NET 3.5• V4.0 - .NET 4.0. - Lazy Loading, POCO, Perf

Enhancements• v4.2 – Bug Fixes - Semantic versioning• v4.3 – Code First• V5.0 – .NET 4.5 - ENums, table-valued

functions, Spatial Data Types• V6.0 – Async Support, Connection Resiliency

Page 18: Entity Framework For DBAs

Why EF?

• Developer works with objects.• Focus on Business Domain (objects) not

DB, Connections, commands, etc.• Developer Productivity• No need to write SQL or CRUD

commands

Page 19: Entity Framework For DBAs

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

Page 20: Entity Framework For DBAs

EF Problems

• Database Generation• N + 1 Problem• Murder on the Index Express• Searching• Caching

Page 21: Entity Framework For DBAs

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.

• DEMO

Page 22: Entity Framework For DBAs

Database Generation

• Things to look for in a ORM generated DB– Normalization– Relationships (They may not exist but should)– Proper Types (NVarchar / BigInt are the exception)– Indexes on FKs– Primary Keys– Table Naming Convention (Singular vs. Plural)– Object Naming (such as Keys and Indexes)

Page 23: Entity Framework For DBAs

N + 1 Problem

• Occurs when object have a 1 : M relationship.

Store Customer

Page 24: Entity Framework For DBAs

N + 1 Fixes

• Use the Includes feature• Use a Stored Procedure

Page 25: Entity Framework For DBAs

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.

Page 26: Entity Framework For DBAs

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

Page 27: Entity Framework For DBAs

Query Complexity

• Complex LINQ queries can generate some awful SQL.

• Cut your losses and replace the LINQ query with a SQL one.

Page 28: Entity Framework For DBAs

Implicit Conversions

• This problem has been mostly fixed in EF 4.1+.

• But you can still run into problems with Code First.

Page 29: Entity Framework For DBAs

Other Tips

• Use a Caching Layer– Redis– ASP.Net has a caching library built in.

• Use a EF Profiler– Hibernating Rhinos– MiniProfiler

Page 30: Entity Framework For DBAs

Reference

Page 31: Entity Framework For DBAs

Other References

• Julie Lerman’s Bloghttp://thedatafarm.com/blog/

• Rowan Miller’s Bloghttp://romiller.com

• Arthur Vicker’s Bloghttp://blog.oneunicorn.com

• #efhelp on twitter• StackOverflow (of course)• PluralSite – Julie Lerman’s EF videos

Page 32: Entity Framework For DBAs

Thank You!!

Richie Rump@Jorrisshttp://jorriss.nethttp://slideshare.net/jorriss http://dotnetmiami.com


Top Related