Just entity framework

1,759 views

Published on

Presented on Trójmiejska Zawodowa Grupa .NET (TGZ.NET)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,759
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Just entity framework

  1. 1. TGZ.NET 14 Dec 2011 Entity Framework „I am just a teenanger”Marcin Dembowski.NET Developer @ Goyello
  2. 2. Agenda• We have a database - DbFirst • The long history • How it works• Model and Code First• Plans for future
  3. 3. PROBLEM: We want to have access from code
  4. 4. ADO.NETEntity Framework
  5. 5. The Hist(e)ory of Ms EF• EDM evolved from E- Relation Modelling (ERM) 1976• Chaos ADO.NET• Announced at TechED TechED in 2006• July 2008 – First release – vs 2008 SP1 .NET 3.5 SP1• April 2010 – Second release• April 2011 – EF 4.1 with CodeFirst• July 2011 – EF 4.1 Update 1 and CTP• November 2011 – EF 4.2 – nothing special 
  6. 6. EF – how it works ? Management, Tracking, Relationships Entity SQL, some kind of facade Our EDMX file Our old ADO.NET Real database
  7. 7. EDMX - metadata
  8. 8. <add name="MyContainer" METADATA connectionString=„ • The Calling Assembly metadata=res://*/MyEFM.csdl| • Referenced assemblies res://*/MyEFM.ssdl| • Assemblies in bin res://*/MyEFM.msl; provider=System.Data.SqlClient; provider connection string=&quot;data source=.R22008EXPRESS; attachdbfilename=|DataDirectory|RepairCompanyDatabase.mdf; integrated security=True;user instance=True; multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />NOTE: The Connection String
  9. 9. Inheritance Strategies• Table Per Hierarchy (TPH)• Table Per Type (TPT)• Table Per Concrete Type (TPC)• Hybrid Mappings• Entity Splitting
  10. 10. Table Per Hierarchy• All properties in one table• Additional discriminator column
  11. 11. PROS: • No JOINS or UNIONS in SQL statements • Flat table CONS: • Derived Properties must be nullable • Denormalized • Poor storage use – many empty fieldsTIP: Table Per Hierarchy (TPH)
  12. 12. Table Per Type• All properties from base type in one table• Additional properties in seperate tables
  13. 13. PROS: • Application is easier to extendCONS: • Performance disadvantage • Uses JOINS • Query can be more complex than required*TIP: Table Per Type (TPT)
  14. 14. Table Per Concrete type• All properties from each type in seperate table• All tables share the same ID (PK)
  15. 15. PROS: • Simple Query when querying for specific typeCONS: • Performance issue when pulling from multiple tables • All keys have to be unique accros tablesTIP: Table Per Concrete type (TPC)
  16. 16. ADO.NETEntity Framework
  17. 17. EF vs. OtherFeature L2S EF NHEager and lazy loading configurable per relationship N N YGood documentation Y Y NEasy logging Y N YMature - N YConfigured by XML N Y/N N*Supports Enums N* YDifferent database providers N Y Y http://msdn.microsoft.com/en-us/library/cc853327.aspx http://blogs.msdn.com/b/adonet/archive/2008/03/27/ado-net-entity-framework-performance-comparison.aspx
  18. 18. PROS: • Any next use is fasterCONS: • First use can take some timestatic readonly Func<MyDataModelContainer, int, IQueryable<Post>> compiledQuery DEFINITION = CompiledQuery.Compile<MyDataModelContainer, int, IQueryable<Post>>((context,id) => from c in context.PostSet where c.UserId == id select c);var post = compiledQuery USAGE .Invoke(context, 1) .FirstOrDefault();TIP: Use compiled query
  19. 19. PROS: • Use this only when you will get some data (Lookup tables)CONS: • Any changes are not tracked PER ENTITY context.UserSet.MergeOption = MergeOption.NoTracking; context.PostSet.MergeOption = MergeOption.NoTracking;TIP: Set MergeOptions
  20. 20. PROS: • Removes the n-select problemCONS: • Can fetch large data from c in context STRINGS .PostSet .Include("PostComment") where c.UserId == id select c from c in context EXPRESSION* .PostSet .Include(c => c.PostComment) where c.UserId == id select cTIP: Including related data (Immediate load)
  21. 21. PROS: • Dynamic Queries • Named parametersCONS: • Type unsafe – it’s uses stringsvar posts = context.PostSet .Where("it.Id > @id", new ObjectParameter("id", 1)) STRINGS .OrderBy("it.Title") .Skip("it.Id", "@skip", new ObjectParameter("skip", 3)) .Top("@limit", new ObjectParameter("limit", 5))TIP: Custom queries
  22. 22. SETTINGS context.ContextOptions.LazyLoadingEnabled = false; context.ContextOptions.ProxyCreationEnabled = false; public partial class Post { public virtual int Id { get; set; } public virtual string Title { get; set; } public virtual string Content { get; set; } … public virtual ICollection<PostComment> PostComment { .. } }TIP: Configure EF behaviour
  23. 23. PROS: • When multiple times used SaveChanges()dbContext.Database.Connection.Open() SETTIP: Manage connections
  24. 24. • Allows to create TPT • Allows to create TPH a • Allows to create database generation script • Allows to create difference migration script http://visualstudiogallery.msdn.microsoft.com/df3541c3-d833-4b65-b942-989e7ec74c87TOOL: Entity Framework Tool
  25. 25. ADO.NETEntity Framework
  26. 26. Tracing• ToTraceString()• Tracing providerhttp://bit.ly/b3AFXc• SQL Profiler AnjLab SQL Profilerhttp://bit.ly/9Igw78
  27. 27. To sum up…• Entity Framework • Removes Linq 2 SQL – don’t* use L2S • Multiple database providers • Database/Model/Code First Design• It’s not so perfect now • It’s like a teenanger … • Still digging into XML • Sometimes you have to use T-SQL instead eSQL
  28. 28. ADO.NET Entity Framework Questions ?Join us : http://kariera.goyello.com
  29. 29. Thanks for your attention !Entity FrameworkMarcin Dembowski• Mail.To("marcin.dembowski") .At("goyello").Com() blog.goyello.com• marcindembowski.wordpress.com• twitter.com/D3M80L twitter.com/goyello facebook.com/goyello kariera.goyello.com

×