entity framework v1 and v2
Post on 07-Nov-2014
4.536 Views
Preview:
DESCRIPTION
TRANSCRIPT
ORM, EDM, ESQL, Entity Framework, LINQ to SQL, LINQ to Entities - confused?Eric NelsonDeveloper & Platform GroupMicrosoft Ltderic.nelson@microsoft.com http://geekswithblogs.net/IUpdateable http://twitter.com/ericnel
htt
p:/
/msd
n.m
icro
soft
.com
/uk/
flash
Editor of the UK MSDN Flash
MSDN Flash eBook 13 of the “Best Technical Articles of 2008”http://bit.ly/flashebook1
MSDN Flash Podcast PilotFor feedbackhttp://bit.ly/flashpod1
Technical Authors wanted for the Flash – 400 to 500 words. Fancy it?
Microsoft UK MSDN Flash NewsletterEvery two weeks, pure joy enters your Inbox
Poll
TipTech
nical
Article
Editori
alFreshDiscoveries
UK Events
The plan...
ORMsSwift look at history and future of ORM on Windows
Entity Framework – with demosEntity Data ModelEntity ServicesObject Services
Entity Framework v2 – with no demosOverview of POCO, Model First etc
ORM?
Data Access over the yearsA
c
c
e
s
s
i
n
g
d
a
t
a
i
n
2
0
0
0
A
D
O
,
S
t
o
r
e
d
P
r
o
c
e
d
u
r
e
s
Accessing data in 2005
ADO.NET, Datasets, DataReaders
Accessing data in 2010
• ORM baby!
Object Relational Mapping
What is it?an Abstractiontechnique for working with relational tables as if they were objects in memoryIntention is to hide away the complexity of the underlying tables and give a uniform way of working with data
Why use it?Developer productivityRetain database independence
Note:Using an ORM does not mean you must use the ORM!
ORM on Windows
Many ORMs out thereLLBLGen Pro http://www.llblgen.com/Nhibernate http://www.hibernate.org/343.htmlEntitySpaces http://www.entityspaces.net/Portal/Default.aspxOpen Access http://www.telerik.com/products/orm.aspx DevForce http://www.ideablade.com/XPO http://www.devexpress.com/Products/NET/ORM/ Lightspeed http://www.mindscape.co.nz/products/LightSpeed/default.aspx Plus many, many more
No clear “winner” = relatively little adoption of ORM
Developers waiting on MicrosoftOf 31 .NET ORMs in 2003, 9 lasted to 2008
It was a long wait...2002 to 2008
Typed Datasets (cough) – shipped ObjectSpaces “v1” – never shipped ObjectSpaces “v2” – never shipped Microsoft Business Framework – never shipped WinFS – never shipped LINQ to SQL - shipped November 2007
Visual Studio 2008 & .NET Framework 3.5
LINQ to Entities - shipped August 2008 Visual Studio 2008 SP1 & .NET Framework 3.5 SP1
Note: LINQ to Entities is the most visible part of the ADO.NET Entity Framework
The future of ORM on Windows
Entity Framework and LINQ to Entities is our strategic technology
Entity Framework v2 in VS2010 and .NET Framework 4.0Best of LINQ to SQL moves into LINQ to Entities
Microsoft is using itData Services - shippingReporting ServicesMicrosoft “M”
Partners supporting itDatabase Vendors – IBM,OpenLink, DataDirect, Devart etcORM vendors supporting it
DevForce now target Entity Framework, replacing their ownLLBLGen v3 will target Entity Framework as well as their ownDevart Entity Developer
It is not just about ORM
{ Simple Walkthough }
demo
LINQ to SQL vs LINQ to Entities
LINQ to SQL LINQ to Entities
Database Support SQL Server SQL Server, DB2, Oracle, Sybase, MySQL ...
Object Relational Mapping Capabilities
Simple Complex
Status Not strategic Strategic
Annoying Rarely Often
Quick Overview
Entity Framework is not just about ORMEntity Framework v1 is being adopted
Typically on applications expected to “live” a long time
Entity Framework is only .NET 3.5 SP1 and aboveEntity Framework is probably not the best choice for a “short lived” SQL Server applicationsEntity Framework v1 has “warts”
Designer It is annoying – buggy, clunkyExposes subset of the functionalityDoes not support model first
N-tier storyStored Procedure SupportForeign KeysPoCoGuidsSQL 2008 New Types
Early adopter feedbackCourtesy of twitter
“EF is still cutting development time in half. Ya gotta love it.”“the entity framework can kiss my damn ass - this shit is ridiculous”
http://bit.ly/EFmarmite
Entity Framework
ADO.NET Entity Framework
Tools and services to create an Entity Data Model (EDM)Tools and services for consuming an Entity Data Model
Entity Data Model
Application modelMapped to a persistence store
Comprised of three layers:
Conceptual (CSDL)Mapping (MSL)Storage (SSDL)
Database agnosticComprised of:
EntitiesAssociationsFunctions
Conceptual
Mapping
Storage
Entity Data Model
{ A look at mapping }
demo
Consuming the EDM1
3
2
Object Services
Entity Client
The ORM
- optional
Entity Client
Familiar ADO.NET object model:EntityCommandEntityConnectionEntityDataReaderEntityParameterEntityTransaction
Text-based resultsRead-onlyUses Entity SQL
Object Services
Queries materialized as ObjectsObjectContextObjectQuery<T>
Built on top of Entity ClientTwo query options:
Entity SQLLINQ
Runtime services:Unit of workIdentity trackingEager/explicit loading
{ Consuming an EDM}
demo
Summary of Entity Framework v1
ORM is an abstraction layer over data
• Productivity• Database Independence
Microsoft has two ORMs
• LINQ to SQL – only SQL Server• Entity Framework (includes LINQ to Entities) – many RDBMS, strategic
ADO.NET Entity Framework includes all the other bits
• EDM, Linq to Entities, ESQL, Object Services, Entity Services• Part of .NET Framework 3.5 SP1
Entity Data Model – mapping from conceptual to store
• CSDL = Conceptual Schema Definition Language• SSDL = Store Schema Definition Language• MSDL = Mapping Schema Definition Language
Two query languages
• ESQL = Entity SQL. Read only query language similar to SQL• LINQ to Entities = Language Integrated Query to the EDM
Entity Framework
V2
Entity Framework v2 in .NET 4.0
For EveryonePluralizationForeign KeysStored ProceduresSelf tracking Entities
For the Model Centric DeveloperModel FirstComplex TypesModel Defined Functions
For the Control FreakCode Generation
For the AgilistPersistence Ignorance, Lazy Loading, Code Only
+ “Other stuff”
Warning – not final. Expect changes!
For Everyone 1 of 4
PluralizationUsed in Database First and Model FirstImplementation
Public abstract PluralizationService Default implementation is English OnlyDefault rules:
EntityTypes / ComplexTypes are singularizedEntitySets are pluralizedNavigation Properties based on cardinality
For Everyone 2 of 4Foreign Keys
Independent Association – v1 Product p = new Product
{ ID = 1,
Name = "Bovril", Category = context.Categories .Single(c => c.Name == "Food")}; context.SaveChanges();
FK Association – v2 Product p = new Product
{ ID = 1, Name = "Bovril", CategoryID = 13 }; context.Products.AddObject(p); context.SaveChanges();
For Everyone 3 of 4
Stored ProceduresStored Procedures as functions
Mapping support for stored procedure resultComplex type as return typeScalar and void return type
Entity CUD using Stored ProceduresNo need to have SPs for all CUD
Self Tracking Entities 4 of 4
Entities do their own change trackingregardless of which tier changes are made on
Somewhere between DTOs and DataSets
For the Model Centric developer
Model FirstStart with a blank model Add entities/relationships (CSDL)Click “Create Database from Model”
Infer Storage Model (SSDL)Infer Mapping (MSL)Infer DDLDeploy DDL
Modify and re-apply. Two way.But - deletes your data!!!
For the Model Centric developer
Complex TypesMap a new type to many typesAdds designer support
Model Defined Functions
Model Level using ESQL<Function Name=“CustomerFullName” ReturnType=“String”>
<Parameter Name=“customer” Type=“MyModel.customer”><DefiningExpression>
customer.FirstName + ‘ ‘ + customer.LastName</DefiningExpression>
</Function>
Enables from c in ctx.Customers select c.FullName()
CLR Level [EdmFunction("MyModel", "CustomerFullName")] public static string FullName(this customer c) { return String.Format("{0} {1}", c.FirstName, c.LastName); }
Enables Console.WriteLine(c.FullName());
For the Control Freak
In V1 we had EntityClassGenerator which could be configured using events
Hard (it used CodeDom)Inflexible (not much control)
In V2 we will haveTemplatedEntityClassGenerator and will ship default T4 templatesCustomization via Tools
Uses Workflow Foundation
For the Agilist/Alt.NET folksPersistence Ignorance
Support for Plain Old CLR ObjectsNo requirement to have specific interface, base class
Lazy LoadingCan not do order.order_details.Load()But Can do lazy loadContext.deferredloading=true
For the Agilist/Alt.NET folksCode Only
No XML files, no model!Convention to config
E.g. Convention: xxxID to a primary key, schema=dboE.g. Config: On map to schema sys and class property to table column
[TableMapping(Schema=“sys”,TableName=“MyDBTable”]
[ColumnMapping(PropertyName=“MyClassProp1”, ColumnName=“table_column1”)]
[Key(PropertyName=“MyClassProp1”)]public objectset<MyThing> MyThings...
And more...
Improved N-Tier APIMore LINQ query operatorsMore SQL generation optimizationsInline functions in ESQLReadonly mapping
Conclusion
•Data Access technology remained reasonably static•Procedural, API access•Surfaced the database schema•No clear ORM choice
1990 to 2008 - Tables
•LINQ – excellent addition to the .NET languages•Entity Framework and the EDM – strategic investment•Productivity leap
2009 – Objects
Resources
http://blogs.msdn.com/efdesignEntity Framework v2
http://geekswithblogs.net/IUpdateableSlides
http://bit.ly/BW1Lq EF Performance
© 2008 Microsoft Ltd. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the
date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
For the framework developer
Driven by Reporting Services team + othersQuery Tree Writing with no objects...
LINQ query without objects...DbExpression
expression=ctx.EntitySet("Orders").Scan().Where(...);DbDataReader reader = ctx.ExecuteQuery(expression);
Or
from c in ctx.EntitySet(“Orders”).Scan()Select c.Property(“ShipCity”)
Readonly mapping<EntityContainerMapping CdmEntityContainer="CContainer"
StorageEntityContainer="SContainer" GenerateUpdateViews="false" >
top related