Professional Documents
Culture Documents
NET Entity
Framework 4.0
Claeys Kurt
CodeCamp.NL
Hi !
• CLAEYS Kurt
– .Net Solution Architect ORDINA.BE
– Community Geek (Visug.be, AZUG.BE)
– Focus on WCF/ADO.NET/Azure/.NET services
– MCT Trainer
– MVP Connected System Developer
– MCSD + MCTS Biztalk/WCF/WF/ADO.NET 3.5
– Mail : Kurt.Claeys@casey.be
– Blog : www.devitect.net
• What is ORM ?
• ADO.NET Entity Framework
• Some patterns
• Code examples & demos
• New in EF 4.0
• More code !
Object – Relational Mapping
Conceptual Mapping Storage
*
*
Objects Relations
Architecture
EF
.NET Entity Provider
Programming (Entity SQL)
Model
Conceptual Model
Quering with LINQ Reader
or Entity SQL Connection
Entity Entity
relationship Command
Updating through the ObjectContext
Mapping (MSL)
V2.0
Mapping layer(MSL)
<ReferentialConstraint>
<Principal Role="Employees"> PK/FK relations
<PropertyRef Name="EmployeeID" />
</Principal>
<Dependent Role="EmployeesTerritories">
<PropertyRef Name="EmployeeID" />
</Dependent>
</ReferentialConstraint>
Conceptual Layer
<EntitySet Name="Categories"
EntityType="NORTHWINDEFModel.Categories" />
... Associations
<AssociationSet Name="FK_Products_Categories"
Association="NORTHWINDEFModel.FK_Products_Categories">
<End Role="Categories" EntitySet="Categories" />
<End Role="Products" EntitySet="Products" />
</AssociationSet>
<EntityType Name="Categories">
Entities / Keys
<Key>
<PropertyRef Name="CategoryID" /> .NET Types
</Key>
<Property Name="CategoryID" Type="Int32" Nullable="false" />
<Property Name="CategoryName" Type="String" Nullable="false“ />
<NavigationProperty Name="Products"
Relationship="NORTHWINDEFModel.FK_Products_Categories"
FromRole="Categories" ToRole="Products" />
</EntityType>
Mapping Layer
Inheritance
<EntityTypeMapping
TypeName="IsTypeOf(NORTHWINDEFModel.DiscontinuedProducts)">
<MappingFragment StoreEntitySet="Products">
<ScalarProperty Name="UnitsOnOrder" ColumnName="UnitsOnOrder" />
<ScalarProperty Name="UnitsInStock" ColumnName="UnitsInStock" />
...
<Condition ColumnName="Discontinued" Value="true" />
</MappingFragment>
</EntityTypeMapping>
Discriminators
TPC Mapping
Fowler : Concrete Table Inheritance Represents an inheritance hierarchy of classes with one
Microsoft : Table Per Concrete Type (TPC)
table per concrete class in the hierarchy
TPT Mapping
Fowler : Class Table Inheritance Represents an inheritance hierarchy of classes with one
Microsoft : Table Per Type (TPT)
table for each class
TPH Mapping
Fowler : Single Table Inheritance Represents an inheritance hierarchy of classes as a single table
Microsoft : Table Per Hierarchy (TPH)
that has columns for all the fields of the various classes.
Object Context
Code
ObjectContext
ChangeTracking
Attach
1. Query
Database
Tables
2. Update the Entities
Notifies
3. Save Changes
Querying
• LINQ
– Compiled
– Type Safe Results
• Avoid JOINs
SELECT c.InvoiceDate, c.InvoiceLines FROM ImvoiceModel.Invoice as c
EF4
• POCO support
• Model First Approach
• LazyLoadingEnabled
• Generation of classes by T4 templates
• Foreign Key Support
• Code only
• Stored Procedures – Function Imports
• Complex Types
EF4 : Persisitence Ignorance - POCO
• PI : An object should not know how it is persisted
– Objects should not depend on data access frameworks to
be used
– Test Driven Development, Domain Driven Design
• POCO : plain old CLR object
– Not inheriting from another public class Supplier
{
class public Int32 Id { get; set; }
public string Name { get; set; }
– No attributes public string City { get; set; }
public List<Product> Product { get; set; }
– Serializable }
Entity Model in
reads the
EDMX metadata from
the model
executed by
T4 Template
Visual Studio
generates code
Generated Classes
EF4 : Generation of classes by T4 templates
//This generated.
namespace MyDomainClasses
{
<#
foreach(EntityTypeWrapper entity in Edm.SourceEntities.OrderBy(e => e.ModelName))
{
#>
public class My<#=entity.ClassName#>
{
<#
foreach(PrimitiveTypePropertyWrapper property in entity.PrimitiveTypeProperties)
{
#>
public <#=property.PropertyType#> <#=property.Name#> { get; set; }
<#
}
#>
}
<#
}
#>
}
EF4: Self tracking entities in N-tier architecture