Successfully reported this slideshow.
Your SlideShare is downloading. ×

Lerman Vvs14 Ef Tips And Tricks

More Related Content

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Lerman Vvs14 Ef Tips And Tricks

  1. 1. VVS14:Entity Framework Tips & Tricks<br />Julie Lerman<br /><br /><br />twitter: @JulieLermanVT<br />
  2. 2. Julie Lerman<br />website<br /><br />blog & twitter<br /><br />book web site<br /><br />consultant/mentor<br />Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader<br />
  3. 3. Tips & Tricks <br />Version 1 Pain Points Fixed in EF4<br />Query Helpers<br />Use ObjectQuery methods with LINQ to Entities<br />Find Exception Handling Details<br />Create Generic Queries<br />Encapsulate Query Execution<br />Improve Query Performance<br />
  4. 4. !<br /> V1 Model hides Foreign Key<br />
  5. 5. Get FK value from Entity Reference<br />
  6. 6. Set FK Value through Entity Ref<br />from Single EntityKey Property<br />Order.CustomerReference.EntityKey =<br /> new EntityKey(&quot;MyEntities.Customers&quot;, &quot;PersonID&quot;, 1)<br />from Multiple EntityKey Properties<br />vareKeyValues =<br /> new KeyValuePair&lt;string, object&gt;[] {<br /> new KeyValuePair&lt;string, object&gt;(“PropertyA&quot;, 12),<br /> new KeyValuePair&lt;string, object&gt;(“PropertyB&quot;, 103)<br /> };<br />EntityKey ekey= new EntityKey(“MyEntities&quot;, eKeyValues);<br />instance.EntityRef.EntityKey=ekey;<br />
  7. 7. V1: Undesirable Store Queries<br />LINQ to Entities Query<br />From p In context.PeopleWhere p.LastName.StartsWith(“K&quot;)<br />Resulting T-SQL Query<br />SELECT [Extent1].[PersonID] AS [PersonID], ...<br />WHERE (CAST(CHARINDEX(N&apos;T&apos;, Extent1].[LastName])<br /> AS int)) = 1<br />!<br />
  8. 8. Control Store Query with ESQL<br /> Entity SQL<br />SELECT VALUE p FROM EFEntities.People AS p<br />WHERE p.LastName LIKE “T%”<br />Query Builder Methodscontext.People.Where(&quot;it.LastName LIKE &apos;T%&apos;&quot;)<br /> Resulting T-SQL<br />SELECT [Extent1].[PersonID] AS [PersonID], <br />WHERE [Extent1].[LastName] LIKE &apos;T%&apos;<br />
  9. 9. Queries in Entity Framework 4<br />Huge improvements to generated queries<br />Contains added to LINQ to Entities & ESQL<br />StartsWith, EndsWith,Contains LIKE %%<br />Much More..<br />See<br />August 5, 2009 post<br />
  10. 10. Awesome Third Party Tools<br />LINQPad<br />Test Queries<br />LINQ, L2S, L2E, ESQL<br />EFProf<br />Capture *All* <br />Database Activity<br />
  11. 11. V1: Random columns from Sprocs<br />CREATE PROCEDURE OrdersBySalesPersonbyDate<br />AS<br />SELECT MIN(Person.FirstName) as First, <br /> MIN(Person.LastName) as Last,<br /> COUNT(OrderId) as OrderCount<br />FROM …<br />Requires lots of manual editing of the model, MSL and SSDL to create an entity that matches the return value<br />!<br />
  12. 12. Use Views in Place of Sprocs<br />CREATE VIEW OrdersBySalesPersonbyDate<br />AS<br />SELECT MIN(Person.FirstName) as First, <br /> MIN(Person.LastName) as Last,<br /> COUNT(Orderid) as OrderCount<br />FROM …<br />Wizard creates all entity metadata elements <br />Other benefits<br />View is composable<br />Use in queries<br />Change Tracking (use stored procs for Ins/Upd/Del)<br />
  13. 13. Stored Procs in Entity Framework 4<br />Major support improvements<br />Map functions to complex types<br />Designer can discover result shapes and auto-create the complex types for you<br />
  14. 14. Country Reference List<br />List&lt;Customer&gt; GetCustomers() <br /> { return context.Customers.OrderBy(c =&gt; c.LastName)<br /> .ToList(); }<br />Account Type Reference List<br /> List&lt;AccountType&gt; GetAccountTypes() <br /> { return context.AccountTypes.OrderBy(a =&gt; a.TypeName)<br /> .ToList(); }<br />Product Reference List<br /> List&lt;Product&gt; GetProducts() <br /> { return context.Products.OrderBy(p =&gt; p. Name)<br /> .ToList(); }<br />!<br /> Redundant L2E Queries<br />
  15. 15. Build Reusable, Generic Queries<br />public static List&lt;TEntity&gt; GetReferenceList&lt;TEntity&gt;<br /> (this ObjectContext context)<br />context.GetReferenceList&lt;Country&gt;<br />context.GetReferenceList(Of AccountType)<br />context.GetReferenceList&lt;Product&gt;<br />
  16. 16. var query=from p in context.People select p;<br />try<br /> { var people=query.ToList(); }<br />catch (EntityException)<br /> { ... }<br />Catch (CommandExecutionException)<br /> { ... }<br />var query2=from o in context.Orders select o;<br />try<br /> { var orders =query2.ToList(); }<br />catch (EntityException)<br /> { ... }<br />Catch (CommandExecutionException)<br /> { ... }<br />!<br /> Redundant code around queries<br />
  17. 17. Encapsulate Query Execution<br />Reusable Repository Methods<br />public List&lt;TEntity&gt; ExecuteList&lt;TEntity&gt;<br /> (ObjectQuery&lt;TEntity&gt; objectQuery)<br />public List&lt;TEntity&gt; ExecuteList&lt;TEntity&gt;<br /> (IQueryable&lt;TEntity&gt; L2EQuery)<br />Calling Reusable Methods<br />IEnumerable&lt;Person&gt; L2EQuery = from p in context.People<br /> select p;<br />List&lt;Person&gt; people = dal.ExecuteList&lt;Person&gt;(L2EQuery);<br />ObjectQuery oQuery = context.Orders;<br />List&lt;Order&gt; orderList = dal.ExecuteList&lt;Order&gt;(oQuery);<br />
  18. 18. s<br />Order B<br />?<br />Person A<br />Person B<br />Order A<br />db Error<br />?<br />Person D<br />Order C<br />?<br />Order D<br />Person C<br />!<br />UpdateExceptions Anonymity<br />
  19. 19. ObjectStateEntry & Entity Instance provided in UpdateException<br />
  20. 20. Pre-Compile L2E Queries<br />Pre-Compiled Query<br />LINQ to Entities<br />Query<br />Store<br />Query<br />
  21. 21. Web Sites Lose Pre-Compiled L2E Queries<br /><ul><li>Short-lived ObjectContext used to compile
  22. 22. Pre-Compilation is repeated each time
  23. 23. App process loses benefit of pre-compilation
  24. 24. Big performance loss</li></ul>!<br />
  25. 25. Cache Pre-Compiled Query Func<br />Class Level Static Function<br />static Func&lt;MyEntities, IQueryable&lt;Customer&gt;&gt; customerPCQ; <br />Class Constructor<br />public CustomerProvider() <br />{<br /> if (customerPCQ == null)<br /> {<br />customerPCQ = CompiledQuery.Compile&lt;MyEntities,<br /> IQueryable&lt;Customer&gt;&gt;<br /> (ctx =&gt; from cust in ctx.Customers<br /> where cust.Orders.Any()<br /> select cust<br /> ); <br /> }<br />} <br />
  26. 26. Summary <br />Some of the big EFV1 pain points are gone in EF4<br />Don’t ignore the power of Entity SQL<br />Dig into the APIs to leverage the real power of EF<br />
  27. 27. Julie Lerman<br />website<br /><br />blog & twitter<br /><br />book web site<br /><br />consultant/mentor<br />Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader<br />
  28. 28. Resources mentioned in this talk<br /><br />,/efdesign, /alexj<br /><br />EF Profiler:<br />