learn entity framework in a day with code first, model first and database first

30
ENTITY FRAMEWORK Mr. Hasan Baloch Mr. Jibran Rasheed Khan Mr. Sehan Ahmed Farooqui

Upload: jibran-rasheed-khan

Post on 13-Apr-2017

759 views

Category:

Technology


0 download

TRANSCRIPT

ENTITY FRAMEWORKMr. Hasan Baloch

Mr. Jibran Rasheed Khan

Mr. Sehan Ahmed Farooqui

OUTLINE

• Introduction to Entity Framework (EF)

• Architecture

• What’s new!

• Different approaches to work with

• Choosing right work model

• Pictorial Tour to each model

• Features & Advantages

• Question & Answer

WHAT IS ENTITY FRAMEWORK???

• EF is a data access framework from Microsoft that helps to bridge the gap between data structures and objects in your applications.

• It is an ORM framework.

• Enhancement to ADO.NET that gives automated mechanism for accessing & storing in the database.

• Microsoft’s latest and recommended way for data access for .NET applications.

ENTITY FRAMEWORK ARCHITECTURE

• EDM, Consists of Conceptual model, Mapping and Storage model.

• LINQ to Entities, Used to write queries against the object model.

• Entity SQL, like L2E, but little more difficult.

• Object Service, Responsible for materialization.

• ECDP, Responsible to convert L2E or Entity SQL queries into a SQL query.

• ADO.net, Communicates with the database using standard ADO

What’s new in EF 5

• Become Open Source

• Enum Support

• Spatial Data Types using DbGeography and DbGeometry classes.

• Table-Valued functions support

• Performance Enhancements

• Designer Improvement

• Better Query Performance

• Code First to add new tables to existing Db

ENTITY FRAMEWORK APPROACHES

DATABASE FIRST

Model the database artifacts (tables, views, primary keys, foreign keys, etc.)

Allows you to use legacy database in your ORM application

Low level control over the database

Entity Data Model can be updated whenever database schema changes.

Database-first approach supports stored procedure, view, etc.

MODEL FIRST

Creating a model using the Entity Framework designer tools

Designer can generate DDL statements to create the database

An easy-to-use UI allows quickly create new entities

Separation of code and database in a declarative format

Dropping and re-creating the database is not a choice

This uses an .EDMX file to store model and mapping information.

CODE FIRST

The primary focus is getting the code and logic

Define entity model with classes and mappings in code..

Entity Framework create (and recreate) the database for you.

Does not use Entity Framework visual design tool, neither to design entities

There is no XML file – no EDMX file –to represent model and storage schemas

CHOOSING RIGHT WORK

MODEL

DATABASE FIRST

PICTORIAL TOUR

HOW TOs

The steps involved in this example are as follows:

Create the new SQL Server database

Create the database objects

Create a new C# Class Library Project in Visual Studio 2012 Solution

Add a new ADO.NET Entity Data Model to project

Create a new Database Connection

Import the database objects into the EDM

Modify the EDM to accommodate the scalar-valued functions

01

0203

04 05

06

07 0809

12

11

10

MODEL FIRST

PICTORIAL TOUR

HOW TOs

The steps involved in this example are as follows:

Creating the model

Creating entities in the .edmx

Editing entity properties

Applying relationships to the entities

Name DatabaseSchema

Generating the database from the model

01 03

02

06

05

04

090807

10

11

12

1514

13

16 17 18

19 20 21

CODE FIRST

PICTORIAL TOUR

HOW TOs

The steps involved in this example are as follows:

Create a new Class Library

Create POCO (Plain Old CLR Object) classes

Add data annotations to the POCO classes.

Create enum classes (if required)

Add Entity Framework to the Solution

Add a DbContext class

Add ‘Code First Migrations’

Updates the database (if needed/made changes)

01 02

0304

04 05

06

07

FEATURES

• Works with a variety of database servers (including Microsoft SQL server, oracle, and DB2)

• Rich mapping engine handle real-world database and work with stored procedure

• Generates strongly-typed entity objects that can be customized beyond 1-1 mapping

• Generates mapping/plumbing code

• Translates LINQ queries to database queries

• Materializes objects from data store calls

• Tracks changes, generating updates/inserts

Advantages

• Reduced development time

• Developers can work more application-centric

• Free from hard-coded dependencies on a particular data engine

• Mappings can be change without changing the application code

• L2Eprovides validation for writing queries against a conceptual model

• Integrates with all the .NET application programming models

• Worked well with ASP.NET, WPF, WCF, and WCF Data Services

Question & Answer“A wise man can learn more from a foolish question than a fool can

learn from a wise answer.”

Bruce Lee

THANK YOUHave Great Time