© copyright sela software & education labs ltd. 14-18 baruch hirsch st. bnei brak 51202 israel...

23
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez Harari Sela Technology Center http://blogs.microsoft.co.il/blogs/erezh/ [email protected]

Post on 21-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

MicrosoftEntity Framework v1.1

overOracle Database

MicrosoftEntity Framework v1.1

overOracle Database

Erez Harari

Sela Technology Center

http://blogs.microsoft.co.il/blogs/erezh/

[email protected]

Page 2: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

AgendaAgenda

• Using data in applications

• Entity Framework WH questions

• Using EF– Designing a model– Querying a model– Working with the model– Data binding support

• What else is there

• Future of EF

Page 3: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Storing application dataStoring application data

• Almost all application stores data

• Data is mostly stored in relation DBs– Data stored in tables– Related data is retrievable via views– Data is stored in a normalized manner– Business-Logic sometimes “leak” to the DB

Page 4: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Modeling the dataModeling the data

• Applications support a more comprehensive model for data – Object Oriented Model– Class & Object (Entity type & instance)– Properties– Inheritance– Complex data-types

Page 5: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Combining the twoCombining the two

• Mapping relational to OO can be sometimes problematic– Write code to load tables into classes– Write code to load Master-Detail relations– Write code to handle Create/Update/Delete (CUD)– Apply appropriate techniques to load & persist inheritance

relations– Apply business logic to compute data after loading

• We had semi-solutions for some of these issues (Typed Datasets)

• We had 3rd party solutions – O/R (Object-Relational) Mapping tools

Page 6: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

What Is Entity Framework?What Is Entity Framework?

• An O/R Mapper

• The next layer in Ado.Net

• An entity model-driven architecture

• The first step in an entity-aware data platform

Page 7: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Why Use Entity Framework?Why Use Entity Framework?

• Why couple your data and your entities?

• Why write a DAL every time?

• Why learn different types of SQL syntaxes?

• Why work hard trying to handle inherited entities?

Page 8: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Where can we find EF?Where can we find EF?

• Part of Visual Studio 2008 SP1

• No special project type is required

Page 9: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

How does EF work?How does EF work?

DevArt tm

dotConnect for Oracle

Page 10: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

DEMODEMOJust a taste

Page 11: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

The Conceptual modelThe Conceptual model

• Conceptual model is based on Entity Model

• Define entity types• Group inherited types to

an entity set• Associate entities

(1:1, 1:N, N:M)

Page 12: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

The Storage modelThe Storage model

• Defines different types of relational data sources:– Table– View– Stored Procedure– Native SQL

• Define functions for create / update / delete (CUD)– Use existing Stored-Procedures– Native SQL

• Import SP that retrieve Entities or Simple types

Page 13: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Conceptual-Storage MappingConceptual-Storage Mapping• Map entity to storage (table, SP…)• Allow filtering source data• Support mapping single entity to multiple

storage sources• Map CUD functions to SP or SQL instead of

auto-generated queries• Support concurrency through original values• Support result binding to query output

Page 14: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

DEMODEMOBuilding a model

Page 15: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Querying EF - eSQLQuerying EF - eSQL

• Brand new query language

• Allow un-typed references – no compilation check

• Can leverage inheritance and collections

• Useful for dynamic query building

Employees model = new Employees();

var query = model.CreateQuery<Person>(

@"Select VALUE p

From Employees.Person as p

Where p.Age > @age");

query.Parameters.Add(new ObjectParameter("age", 30));

foreach (Person p in query)

{

Console.WriteLine(p.FirstName +

" " + p.LastName );

}

Employees model = new Employees();

var query = model.CreateQuery<Person>(

@"Select VALUE p

From Employees.Person as p

Where p.Age > @age");

query.Parameters.Add(new ObjectParameter("age", 30));

foreach (Person p in query)

{

Console.WriteLine(p.FirstName +

" " + p.LastName );

}

Page 16: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Querying EF – Extension MethodsQuerying EF – Extension Methods

• Query model against EDM

• Typed references, using lambda expressions

• Compiler verified• Can return

anonymous types• A bit hard to read

Employees model = new Employees();

int age = 30;

var query = model.Person.

Where(p => p.Age > age).

Select(item => new {

FullName = item.FirstName +

" " + item.LastName });

foreach (var p in query)

{

Console.WriteLine(p.FullName);

}

Employees model = new Employees();

int age = 30;

var query = model.Person.

Where(p => p.Age > age).

Select(item => new {

FullName = item.FirstName +

" " + item.LastName });

foreach (var p in query)

{

Console.WriteLine(p.FullName);

}

Page 17: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Querying EF – LINQ to EntitiesQuerying EF – LINQ to Entities

• LINQ-based syntax• Compiles as set of

extension methods• Much more readable

than extension methods

• Makes building enhanced queries a lot easier

Employees model = new Employees();

int age = 30;

var query =

from p in model.Person

where p.Age > age

select new {

FullName = p.FirstName +

" " + p.LastName};

foreach (var p in query)

{

Console.WriteLine(p.FullName);

}

Employees model = new Employees();

int age = 30;

var query =

from p in model.Person

where p.Age > age

select new {

FullName = p.FirstName +

" " + p.LastName};

foreach (var p in query)

{

Console.WriteLine(p.FullName);

}

101 LINQ Sampleshttp://tinyurl.com/62j6sb

Page 18: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Querying - behind the scenesQuerying - behind the scenesfrom p in model.Person

where p.Age > age

select new {FullName = p.FirstName

+ " " + p.LastName};

from p in model.Person

where p.Age > age

select new {FullName = p.FirstName

+ " " + p.LastName};

model.Person.

Where(p => p.Age > age).

Select(p => new {FullName =

p.FirstName + " " + p.LastName });

model.Person.

Where(p => p.Age > age).

Select(p => new {FullName =

p.FirstName + " " + p.LastName });

model.CreateQuery<DbDataRecord>(

@"Select p.FirstName + "" ""

+ p.LastName as FullName

From Employees.Person as p

Where p.Age > @age");

model.CreateQuery<DbDataRecord>(

@"Select p.FirstName + "" ""

+ p.LastName as FullName

From Employees.Person as p

Where p.Age > @age");

Query tree structureQuery tree structure

SELECT 1 AS [C1], [Extent1].[FirstName] + ' ' + [Extent1].[LastName] AS [C2]

FROM [dbo].[Person] AS [Extent1]

WHERE [Extent1].[Age] > @age

SELECT 1 AS [C1], [Extent1].[FirstName] + ' ' + [Extent1].[LastName] AS [C2]

FROM [dbo].[Person] AS [Extent1]

WHERE [Extent1].[Age] > @age

Page 19: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

DEMODEMOQuerying the model

Page 20: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Transactions and ConcurrencyTransactions and Concurrency

• Transactions– All updates executed in .SaveChanges() are wrapped in a

transaction– Fully integrated with System.Transactions– Supports explicitly transaction usage

• Concurrency– Uses Ado.Net Optimistic concurrency checks (off by default)– Using non-default update & delete queries (Native-SQL, SP…)

allows access to current & original data

Page 21: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Working with layersWorking with layers

• EF objects can be detached from & attached to a context

• EF objects support XML serialization– XmlSerializer– DataContractSerializer

• Can be consumed through different service types:– ASP.NET Web Services– WCF Services– Ado.Net Data Services (also support JSON

serialization)

Page 22: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

SummarySummary

• No more need to write DAL code• Conceptual model satisfied using EDM• CRUD operations support with no

additional code needed• Can be LINQed and queried in various

ways• Conceals, but doesn’t prevent – you

can still use your DB code (SQL / SP) if required to

Page 23: © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel Microsoft Entity Framework v1.1 over Oracle Database Erez

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

ResourcesResources

• ADO.NET Entity Framework– Data platform developer center

http://msdn.microsoft.com/en-us/data/aa937723.aspx

– Ado.Net team Bloghttp://blogs.msdn.com/adonet

– Forumhttp://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=533&SiteID=1

• ADO.NET Entity Framework V2 Design http://blogs.msdn.com/efdesign

• Contact Me– http://blogs.microsoft.co.il/blogs/erezh– [email protected]