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.
3. • 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.
9. • Object Relational Mapping
• Converts pragmatic objects into a
relational database.
– Hibernate (Java)
– Active Record (Ruby)
Objects Mapping Data
10. • 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.
11.
12.
13. 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();
}
15. Lovingly stolen from Programming Entity Framework: Code First by Julie Lerman and Rowan Miller page 9.
16. Design Centric Code Centric
New
Database
Existing
Database
Model First
Create .edmx model in designer
Generate DB from .edmx
Classes auto-generate from
.edmx
Database First
Reverse engineer .edmx model
Classes auto-generate from
.edmx
Code First
Define classes & mapping in code
Database auto-created at
runtime
Code First
Define classes & mapping in code
Adapted from Programming Entity Framework: Code First by Julie Learman and Rowan Miller page 3.
18. • Developer works with objects.
• Focus on Business Domain (objects) not
DB, Connections, commands, etc.
• Developer Productivity
• No need to write SQL or CRUD
commands
19. • 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
21. • 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
22. • 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)
23. • Occurs when object have a 1 : M
relationship.
Store Customer
24. • Use the Includes feature
• Use a Stored Procedure
25. • 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.
26. • 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
27. • Complex LINQ queries can generate
some awful SQL.
• Cut your losses and replace the LINQ
query with a SQL one.
28. • This problem has been mostly fixed in EF
4.1+.
• But you can still run into problems with
Code First.
29. • Use a Caching Layer
– Redis
– ASP.Net has a caching library built in.
• Use a EF Profiler
– Hibernating Rhinos
– MiniProfiler
30.
31. • Julie Lerman’s Blog
http://thedatafarm.com/blog/
• Rowan Miller’s Blog
http://romiller.com
• Arthur Vicker’s Blog
http://blog.oneunicorn.com
• #efhelp on twitter
• StackOverflow (of course)
• PluralSite – Julie Lerman’s EF videos