entity framework for dbas
DESCRIPTION
Do you have applications in your environment that don't use SQL written by a person but by the application itself? No, the system isn't self-aware. Chances are the system is utilizing Microsoft's Entity Framework. Entity Framework (EF) and other object relational mapping technologies have been a boon for developers. But is Entity Framework a technology that developers should even be using? In this session, we'll review what Entity Framework is and how it's changed over time. We'll also dive into how Entity Framework works and what to look for when inspecting a database generated by Entity Framework. Finally, we'll review T-SQL generated by Entity Framework and give some tips on how to improve performance. If you're a data professional that manages databases that are accessed through Entity Framework or would like a basic knowledge of how Entity Framework works this session is for you.TRANSCRIPT
Entity Framework: Or How I Learned to Stop Worrying and
Learned to Love DevelopersRichie Rump
Jorriss LLC@Jorriss
www.jorriss.net
Who is this dude?
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.
2008
Photo Credit: http://www.patrickfallonphoto.com/2008/11/04/election-2008-barack-obamas-election-night-grant-park/
2008
Photo Credit: http://www.sacbee.com/static/weblogs/photos/2008/08/014666.html
2008
Photo Credit: http://triggerpit.com/2010/11/22/incredible-pics-nasa-astronaut-wheelock/
2008
Photo Credit: http://www.nydailynews.com/sports/football/giants/eli-manning-making-quarterback-article-1.1013353
2008
Photo Credit: http://www.businessinsider.com/how-burger-king-went-from-mcdonalds-greatest-rival-to-total-train-wreck-2012-4
What is an ORM
• Object Relational Mapping• Converts pragmatic objects into a
relational database.– Hibernate (Java)– Active Record (Ruby)
Objects Mapping Data
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.
How to use EF: Designer
How to use EF: Code
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();}
Now With EF
var stores = context.Stores.Include(c => c.Customers);
So how does this thing work?
Lovingly stolen from Programming Entity Framework: Code First by Julie Lerman and Rowan Miller page 9.
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.
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
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
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
EF Problems
• Database Generation• N + 1 Problem• Murder on the Index Express• Searching• Caching
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
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)
N + 1 Problem
• Occurs when object have a 1 : M relationship.
Store Customer
N + 1 Fixes
• Use the Includes feature• Use a Stored Procedure
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.
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
Query Complexity
• Complex LINQ queries can generate some awful SQL.
• Cut your losses and replace the LINQ query with a SQL one.
Implicit Conversions
• This problem has been mostly fixed in EF 4.1+.
• But you can still run into problems with Code First.
Other Tips
• Use a Caching Layer– Redis– ASP.Net has a caching library built in.
• Use a EF Profiler– Hibernating Rhinos– MiniProfiler
Reference
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
Thank You!!
Richie Rump@Jorrisshttp://jorriss.nethttp://slideshare.net/jorriss http://dotnetmiami.com