2. Julie Lerman website theDataFarm.com blog & twitter theDataFarm.com/blog@julielermanVT book web site LearnEntityFramework.com consultant/mentor Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader
3. Tips & Tricks Version 1 Pain Points Fixed in EF4 Query Helpers Use ObjectQuery methods with LINQ to Entities Find Exception Handling Details Create Generic Queries Encapsulate Query Execution Improve Query Performance
6. Set FK Value through Entity Ref from Single EntityKey Property Order.CustomerReference.EntityKey = new EntityKey("MyEntities.Customers", "PersonID", 1) from Multiple EntityKey Properties vareKeyValues = new KeyValuePair<string, object>[] { new KeyValuePair<string, object>(“PropertyA", 12), new KeyValuePair<string, object>(“PropertyB", 103) }; EntityKey ekey= new EntityKey(“MyEntities", eKeyValues); instance.EntityRef.EntityKey=ekey;
7. V1: Undesirable Store Queries LINQ to Entities Query From p In context.PeopleWhere p.LastName.StartsWith(“K") Resulting T-SQL Query SELECT [Extent1].[PersonID] AS [PersonID], ... WHERE (CAST(CHARINDEX(N'T', Extent1].[LastName]) AS int)) = 1 !
8. Control Store Query with ESQL Entity SQL SELECT VALUE p FROM EFEntities.People AS p WHERE p.LastName LIKE “T%” Query Builder Methodscontext.People.Where("it.LastName LIKE 'T%'") Resulting T-SQL SELECT [Extent1].[PersonID] AS [PersonID], WHERE [Extent1].[LastName] LIKE 'T%'
9. Queries in Entity Framework 4 Huge improvements to generated queries Contains added to LINQ to Entities & ESQL StartsWith, EndsWith,Contains LIKE %% Much More.. See blogs.msdn.com/adonet August 5, 2009 post
10. Awesome Third Party Tools LINQPad Test Queries LINQ, L2S, L2E, ESQL EFProf Capture *All* Database Activity
11. V1: Random columns from Sprocs CREATE PROCEDURE OrdersBySalesPersonbyDate AS SELECT MIN(Person.FirstName) as First, MIN(Person.LastName) as Last, COUNT(OrderId) as OrderCount FROM … Requires lots of manual editing of the model, MSL and SSDL to create an entity that matches the return value !
12. Use Views in Place of Sprocs CREATE VIEW OrdersBySalesPersonbyDate AS SELECT MIN(Person.FirstName) as First, MIN(Person.LastName) as Last, COUNT(Orderid) as OrderCount FROM … Wizard creates all entity metadata elements Other benefits View is composable Use in queries Change Tracking (use stored procs for Ins/Upd/Del)
13. Stored Procs in Entity Framework 4 Major support improvements Map functions to complex types Designer can discover result shapes and auto-create the complex types for you
14. Country Reference List List<Customer> GetCustomers() { return context.Customers.OrderBy(c => c.LastName) .ToList(); } Account Type Reference List List<AccountType> GetAccountTypes() { return context.AccountTypes.OrderBy(a => a.TypeName) .ToList(); } Product Reference List List<Product> GetProducts() { return context.Products.OrderBy(p => p. Name) .ToList(); } ! Redundant L2E Queries
25. Cache Pre-Compiled Query Func Class Level Static Function static Func<MyEntities, IQueryable<Customer>> customerPCQ; Class Constructor public CustomerProvider() { if (customerPCQ == null) { customerPCQ = CompiledQuery.Compile<MyEntities, IQueryable<Customer>> (ctx => from cust in ctx.Customers where cust.Orders.Any() select cust ); } }
26. Summary Some of the big EFV1 pain points are gone in EF4 Don’t ignore the power of Entity SQL Dig into the APIs to leverage the real power of EF
27. Julie Lerman website theDataFarm.com blog & twitter theDataFarm.com/blog@julielermanVT book web site LearnEntityFramework.com consultant/mentor Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader
28. Resources mentioned in this talk Learnentityframework.com Blogs.msdn.com/adonet,/efdesign, /alexj www.linqpad.net EF Profiler: http://ayende.com/Blog/category/551.aspx