Professional Documents
Culture Documents
ADO Net Entity Framework 4 0
ADO Net Entity Framework 4 0
ADO Net Entity Framework 4 0
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
Im from Belgium !
Agenda (1.15)
What is ORM ? ADO.NET Entity Framework Some patterns Code examples & demos New in EF 4.0 More code !
* *
Objects
Relations
Architecture
EF
Connection
Entity
Command
Mapping (MSL)
V2.0
Store
3 Layers
Conceptual layer(CSDL) The conceptual entity model that represents the entities and their associations and inheritance structure = Domain Model
Mapping layer(MSL) The metadata needed to map the conceptual model to the logical model. Storage layer (SSDL) The logical store model that represents the relational schema from a database
Storage Layer
Tables + PK <EntityType Name="Employees"> <Key> SQL <PropertyRef Name="EmployeeID" /> Field and Types </Key> <Property Name="Address" Type="nvarchar" MaxLength="60" /> ... <Property Name="TitleOfCourtesy" Type="nvarchar" MaxLength="25" /> </EntityType> <ReferentialConstraint> <Principal Role="Employees"> <PropertyRef Name="EmployeeID" /> </Principal> <Dependent Role="EmployeesTerritories"> <PropertyRef Name="EmployeeID" /> </Dependent> </ReferentialConstraint>
PK/FK relations
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 Microsoft : Table Per Concrete Type (TPC)
Represents an inheritance hierarchy of classes with one table per concrete class in the hierarchy
TPT Mapping
Fowler : Class Table Inheritance Microsoft : Table Per Type (TPT)
Represents an inheritance hierarchy of classes with one table for each class
TPH Mapping
Fowler : Single Table Inheritance Microsoft : Table Per Hierarchy (TPH)
Represents an inheritance hierarchy of classes as a single table 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
Entity SQL
SQL alike Query language Can do the same as LINQ (and vice versa) Queries on the model, not the database. Object Oriented
dot syntax, TypeOf() Is translated to T-SQL by EF (in combination with driver of vendor)
SELECT VALUE il FROM InvoiceLines AS il WHERE il.Invoice.InvoiceCustomerID = 99
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
Generated Classes