O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Entity Framework Tutorial: Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services

Book Description

In Detail

The ADO.NET Entity Framework is a new way to build the data access layer of your Windows or web applications. It's an Object Relational Mapping (ORM) technology that makes it easy to tie together the data in your database with the objects in your applications, by abstracting the object model of an application from its relational or logical model.

This clear and concise book gets you started with the Entity Framework and carefully gives you the skills to speed up your application development by constructing a better data access layer. It shows you how to get the most from the ADO.NET Entity Framework to perform CRUD operations with complex data in your applications.

This tutorial starts out with the basics of the Entity Framework, showing plenty of examples to get you started using it in your own code. You will learn how to create an Entity Data Model, and then take this further with Entity types. You will also learn about the Entity Client data provider, learn how to create statements in Entity SQL, and get to grips with ADO.NET Data Services, also known as Project Astoria.

What you will learn from this book?

  • Explore the features of the ADO.NET Entity Framework

  • Understand the concepts of entities and their relationships

  • Create an Entity Data Model using the ADO.NET Entity Data Model Designer as well as the EdmGen tool

  • Get started with the Entity Framework and implement your application using it

  • Extend the existing Entity Types to create your own Entity Types

  • Write programs against the Entity Data Model to perform CRUD operations

  • Explore ADO.NET Data Services and learn how they can be used with the Entity Framework


This book is written as a tutorial, leading you through examples to illuminate the main theory and get you working with the Entity Framework as quickly as possible.

Who this book is written for?

This book is for C# developers who want an easier way to create their data access layer. You will need to be comfortable with ADO.NET, but you don't need to know anything about the Entity Framework. Along the way we will create some ASP.NET applications, so being familiar with this will also help.

Table of Contents

  1. Entity Framework Tutorial
  2. Credits
  3. About the Author
  4. About the Reviewer
  5. Preface
    1. What This Book Covers
    2. What You Need for This Book
    3. Who This Book is For
    4. Conventions
    5. Reader Feedback
    6. Customer Support
      1. Downloading the Example Code for the Book
      2. Errors
      3. Piracy
      4. Questions
  6. 1. Introducing the ADO.NET Entity Framework
    1. What You should Know
    2. Looking Back
    3. What is ADO.NET Entity Framework?
      1. Is It Just Another ORM?
    4. The ADO.NET Entity Framework Architectural Components
    5. The Entity Data Model (EDM)
      1. How is the Entity Data Model Represented?
      2. The Object Model (O-Space)
      3. LINQ to Entities
      4. Entity Client
      5. Entity SQL
        1. Avoiding Complex Joins
      6. The Object Services Layer
      7. ADO.NET Entity Framework—Features and Benefits at a Glance
    6. Installing the Prerequisites
      1. Installing the ADO.NET Entity Framework and Its Tools
        1. Downloading the Software
        2. Installing the Software
    7. Designing the Payroll Database
    8. Summary
    9. Glossary
  7. 2. Getting Started
    1. Creating an Entity Data Model
      1. Creating the Payroll Entity Data Model Using the ADO.NET Entity Data Model Designer
      2. Creating the Payroll Data Model Using the EdmGen Tool
    2. The ADO.NET Entity Data Source Control
    3. Implementing Our First Application Using the Entity Framework
    4. Summary
  8. 3. Entities, Relationships, and the Entity Data Model
    1. Entities, Entity Types, and Relationships in the ADO.NET Entity Data Model (EDM)
      1. What is an Entity?
      2. Defining Entity Sets in the Entity Data Model
      3. Extending the Existing Entity Types to Create Derived Entity Types
      4. Association Sets, Associations, Containment, and Multiplicity
      5. What are Entity Containers?
    2. Exploring the Payroll Entity Data Model
      1. The Mapping Details Window
      2. The Entity Model Browser
      3. The Entity Data Model Layers
        1. The CSDL Schema
        2. The SSDL Schema
        3. The MSL Schema
    3. Summary
  9. 4. Working with Stored Procedures in the Entity Data Model
    1. Mapping Stored Procedures to Functions in the EDM
      1. Mapping Create, Update, and Delete Functions to Entities in the EDM
      2. Mapping the Association Sets Consistently
      3. Mapping Stored Procedures with No Entity Set
    2. Using Stored Procedures
    3. Mapping Stored Procedures that Return Custom Entity Types
    4. Summary
  10. 5. Working with Entity Client and Entity SQL
    1. An Overview of the Entity SQL Language
      1. From Transact SQL (T-SQL) to Entity SQL (E-SQL)
      2. Why Entity SQL When I Already have LINQ to Entities?
      3. Features of Entity SQL
      4. Operators in Entity SQL
        1. Arithmetic Operators
        2. Comparison Operators
        3. Logical Operators
        4. Reference Operators
        5. Type Operators
        6. Set Operators
        7. Operator Precedence
      5. Expressions in Entity SQL
        1. Query Expressions in Entity SQL
      6. Identifiers, Variables, Parameters, and Types in Entity SQL
        1. Row
        2. Collection
        3. Reference
      7. Canonical Functions in Entity SQL
        1. Mathematical Functions
        2. Aggregate Functions
        3. String Functions
        4. Bitwise Functions
        5. Date and Time Functions
    2. Data Paging Using Entity SQL
    3. Working with the ADO.NET Entity Client
      1. Let's Get into Action
        1. Building the Connection String
        2. Creating an Entity Connection
        3. Opening the Connection
        4. Executing Queries Using the Entity Command
        5. Closing the Connection
    4. Other Operations with Entity SQL
      1. Inserting a Record Using Entity SQL
      2. Inserting a Record with a Foreign Key Constraint
      3. Retrieving Native SQL from EntityCommand
      4. Transaction Management in Entity SQL
    5. Summary
  11. 6. Working with LINQ to Entities
    1. Introducing LINQ
      1. Why LINQ?
    2. Understanding the LINQ Architecture
      1. LINQ to XML
      2. LINQ to SQL
      3. LINQ to Objects
      4. LINQ to Entities
        1. Querying Data Using LINQ to Entities
        2. LINQ to Entities and Entity Framework
        3. Differences between LINQ to Entities and LINQ to SQL
    3. Operators in LINQ
      1. Aggregation
      2. Projections
      3. Ordering
      4. Quantifiers
      5. Restriction
      6. Conversion
      7. Element
      8. Set
    4. Querying Data Using LINQ
      1. Expressions in LINQ to Entities
        1. Constant Expressions
        2. Comparison Expressions
        3. Initializing Expressions
        4. Null Comparisons
        5. Navigation Properties
        6. Immediate and Deferred Query Execution
        7. Improving Performance with Compiled Queries
    5. Summary
  12. 7. Working with the Object Services Layer
    1. What are Object Services?
      1. Features at a Glance
      2. A Quick Look at the ObjectContext Class in our Payroll EDM
      3. Querying Data as in-Memory Objects
    2. Adding, Modifying, and Deleting Objects
    3. Attaching and Detaching Objects to and from the Object Context
    4. Serializing and De-Serializing Entity Instances
    5. Change Tracking and Identity Resolution Using ObjectContext
      1. Implementing a Sample Application
      2. Creating the Form
      3. Implementing a Custom DataContext
      4. How is This Accomplished?
        1. Updating an Employee Record
        2. Handling Concurrency Conflicts
    6. Inheritance in the Entity Framework
      1. Table per Hierarchy
      2. Table per Type
      3. Table per Concrete Type
    7. Implementing Complex Types in the EDM
    8. Summary
  13. 8. Introducing ADO.NET Data Services
    1. Introducing ADO.NET Data Services
      1. How Do ADO.NET Data Services and Web Services Differ?
      2. What is Representational State Transfer (REST)?
      3. Why Use ADO.NET Data Services?
        1. Features at a Glance
    2. Prerequisites
    3. Exposing Data as a Service Using ADO.NET Data Services
      1. Creating an ADO.NET Data Service
      2. Using a Relational Database as the Data Source
      3. Using Data Sources Other Than a Relational Database
    4. Understanding the System.Services.Data Namespace
    5. Restricting Access to Resources
    6. Working with the ADO.NET Data Service Client Library
      1. Generating the Client-Side Entity Classes
      2. Inserting a Record
      3. Updating a Record
      4. Deleting a Record
      5. Consuming an ADO.NET Data Service Using LINQ
      6. Exposing a Stored Procedure as a URI
    7. Handling Exceptions in ADO.NET Data Services
    8. Batching ADO.NET Data Services Requests to Improve Performance
      1. Debugging Your Data Service
    9. References
    10. Summary