Entity framework and how to use it

3,670 views

Published on

TG.NET meeting april 2011

Published in: Technology
  • Be the first to comment

Entity framework and how to use it

  1. 1. Entity Framework 4& How touseit<br />Pascal Fransen<br />April 2011<br />Nspyre Technology Group .NET<br />www.nspyre.net<br />
  2. 2. Overview<br /><ul><li>Entity Framework
  3. 3. Introduction
  4. 4. Features
  5. 5. Code first
  6. 6. Demo : Forms
  7. 7. Model first
  8. 8. Demo : Console
  9. 9. Generate From database
  10. 10. Tiny Introduction MVC3
  11. 11. Tiny Introduction Razor
  12. 12. Demo : EF4/MVC3/razor
  13. 13. Questions</li></ul>2<br />
  14. 14. EntityFramework - introduction<br />3<br />
  15. 15. EntityFramework – introduction – new features<br />4<br /><ul><li>POCO Support: You can now define entities without requiring base classes or data persistence attributes.
  16. 16. Lazy Loading Support: You can now load sub-objects of a model on demand instead of loading them up front.
  17. 17. N-Tier Support and Self-Tracking Entities: Handle scenarios where entities flow across tiers or stateless web calls.
  18. 18. Better SQL Generation and SPROC support: EF4 executes better SQL, and includes better integration with SPROCs
  19. 19. Automatic Pluralization Support: EF4 includes automatic pluralization support of tables (e.g. Categories->Category).
  20. 20. Improved Testability: EF4’s object context can now be more easily faked using interfaces.
  21. 21. Improved LINQ Operator Support: EF4 now offers full support for LINQ operators. </li></li></ul><li>Entity Framework – Code First<br />Addreferences via Tools -> Library Package Manager -> Package Manager Console<br />In console type install-package entityframework<br />Referenceswillbeaddedautomatically<br />5<br />
  22. 22. EntityFramework – Code First -Create data classes sets and context<br />Create Classes<br />Addreferencestoother classes (virtual)<br />CreateDbContextwithDbSet ‘s<br />6<br />usingSystem.Data.Entity;<br />namespaceEntityCodeFirst<br />{<br />public class Restaurant : DbContext<br /> {<br />public DbSet<Table> Tables { get; set; }<br />public DbSet<Client> Clients { get; set; }<br /> }<br />}<br />public class Client<br />{<br />public string Name { get; set; }<br />public string Phone { get; set; }<br />public DateTime Time { get; set; }<br /> public virtual Table Table { get; set; }<br />}<br />public class Table<br />{<br />public string Name { get; set; }<br />public intMaxClients { get; set; }<br />public virtual ICollection<Client> Clients{ get; set; } <br />}<br />
  23. 23. Entity Framework – Code First - Addconnection string<br />Addto web / app.config :<br /><?xmlversion="1.0" encoding="utf-8" ?><br /><configuration><br /> <connectionStrings><br /> <add name="Restaurant"<br />connectionString="Server=.SQLEXPRESS;Database=Restaurant;Trusted_Connection=True;"<br />providerName="System.Data.SqlClient" /><br /> </connectionStrings><br /></configuration><br />7<br />
  24. 24. Entity Framework – Code First - Demo<br />Demo<br />8<br />
  25. 25. Entity Framework – Model First<br />Class Library project<br />Add New Item : ADO.NET Entity Data Model<br />Select : Empty model<br />Use Right-click or Toolboxtoadd:<br />Entities<br />Associations<br />Inheritance<br />Always use ‘createkey property’<br />9<br />
  26. 26. Entity Framework – Model First - Associations<br />Choose name<br />Set multiplicity<br />Addforeignkey:<br />willautomaticallyadd Ids<br />Referentialconstraint is added<br />10<br />
  27. 27. Entity Framework – Model First - Generateto database<br />Database Generation Workflow <br />Default : TablePerTypeStrategy.xaml(VS)<br />More availablefromPowerPack<br />Generate Database from Model<br />A database must exist<br />Choose Data Connection<br />DDL is generatedandsaved<br />Put it in your source control<br />App.config is added<br />Check the tablenames … <br />11<br />
  28. 28. Entity Framework- Model First - Use model project<br />Addreferenceto model project<br />AddconnectionstringtoApp.config<br />Or just copy-paste itwhen empty<br />AddreferencetoSystem.Data.Entity<br /> var context = new ModelFirstDemo.FirstModelContainer();<br />varquery = from c in context.PersonSet select c;<br />foreach(Person p in query.ToList())<br />{<br />Console.WriteLine(p.Name);<br />}<br />12<br />
  29. 29. Entity Framework- Model First - Demo<br />Demo<br />13<br />
  30. 30. Entity Framework – new MVC Project<br />Add ASP.NET MVC3 Web Application<br />Choose : Empty<br />View Engine : Razor<br />Addreferences:<br />Toentityproject<br />ToSystem.Data.Entity<br />InsertconnectionstringintoWeb.Config<br />14<br />
  31. 31. MVC3 - Tiny Introduction<br />Clean Separation of Concerns, where each component is serving only one purpose. <br />Thus it also gives us the opportunity to integrate TDD (Test-Driven Development) in the development workflow and unit test each component without considering the others, as most of the framework components are interface-based. This allows us to mock them out.<br />The whole framework is very much extensible and pluggable. It is really easy to replace or customize each part without affecting the others.<br />Pretty/SEO (Search Engine Optimization) URLs. Full controls of how URLs are constructed. No need to do URL Rewriting anymore.<br />True stateless model of the web. We no longer have <br /> to deal with postbacksand ViewState.<br />Full control of the generated HTML. <br />Utilize the existing knowledge of ASP.NET like Providers, Caching,Configuration <br />15<br />
  32. 32. MVC3 – Tiny Razorintroduction<br />Alternativefor asp.net view engine<br />Clean coding model<br />Integratesnicelywith HTML<br />‘Master Page’-like features<br />16<br /><p>My name is @View.Name and I live at @View.Address</p><br />Compare this to the equivalent using the aspx view engine<br /><p>My name is <%:View.Name %> and I live at <%: View.Address %></p><br />
  33. 33. Entity Framework - Database First -Generatefrom database<br />Choosegeneratefrom database<br />17<br />
  34. 34. MVC3 - Add controller and view<br />Model is available in entity project<br />Controller puts data in View<br />Views canbeaddedby right-clicking the ActionResult<br />View name is generated<br />ChooseRazor view engine<br />Create a strongly-typed view -><br />18<br />
  35. 35. MVC3 – build the view<br />Somehow, the view does notknowabout the Entity Framework, soaddtoweb.config<br /><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/><br />Model passedthroughconstructor<br />Model properties are availablewithinrazor view<br />19<br />
  36. 36. MVC3 – lists and editing<br />New controller withgenerated Action methods<br />Choose the entityandscaffoldtemplate:<br />Create<br />Delete<br />Details<br />Edit<br />Empty<br />List<br />20<br />
  37. 37. Model First - Stored Procedures<br />Right-click object andchoose<br />Allthree CUD procedures must bemapped<br />21<br />
  38. 38. MVC3/Razor/EF4<br />Demo<br />22<br />
  39. 39. References<br />Entity Framework<br />http://msdn.microsoft.com/en-us/data/aa937723<br />MVC 3<br />http://www.asp.net/mvc/mvc3<br />http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs<br />Entity Designer Database Generation Power Pack<br />http://visualstudiogallery.msdn.microsoft.com/df3541c3-d833-4b65-b942-989e7ec74c87/<br />NuGet package management<br />http://nuget.codeplex.com/<br />23<br />
  40. 40. Questions?<br />24<br />

×