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. EF – how it works ?
Management, Tracking,
Relationships
Entity SQL, some kind of facade
Our EDMX file
Our old ADO.NET
Real database
11. PROS: • No JOINS or UNIONS in SQL statements
• Flat table
CONS: • Derived Properties must be nullable
• Denormalized
• Poor storage use – many empty fields
TIP: Table Per Hierarchy (TPH)
12. Table Per Type
• All properties from base type in one table
• Additional properties in seperate tables
13. PROS: • Application is easier to extend
CONS: • Performance disadvantage
• Uses JOINS
• Query can be more complex than required*
TIP: Table Per Type (TPT)
14. Table Per Concrete type
• All properties from each type in seperate table
• All tables share the same ID (PK)
15. PROS: • Simple Query when querying for specific type
CONS: • Performance issue when pulling from multiple tables
• All keys have to be unique accros tables
TIP: Table Per Concrete type (TPC)
17. EF vs. Other
Feature L2S EF NH
Eager and lazy loading configurable per relationship N N Y
Good documentation Y Y N
Easy logging Y N Y
Mature - N Y
Configured by XML N Y/N N*
Supports Enums N* Y
Different 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. PROS: • Any next use is faster
CONS: • First use can take some time
static 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. 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. PROS: • Removes the n-select problem
CONS: • 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 c
TIP: Including related data (Immediate load)
21. PROS: • Dynamic Queries
• Named parameters
CONS: • Type unsafe – it’s uses strings
var 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. 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. PROS: • When multiple times used SaveChanges()
dbContext.Database.Connection.Open()
SET
TIP: Manage connections
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-989e7ec74c87
TOOL: Entity Framework Tool
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