Book Image

Entity Framework Tutorial (Update) - Second Edition

By : Joydip Kanjilal
Book Image

Entity Framework Tutorial (Update) - Second Edition

By: Joydip Kanjilal

Overview of this book

The ADO.NET Entity Framework from Microsoft is a new ADO.NET development framework that provides a level of abstraction for data access strategies and solves the impedance mismatch issues that exist between different data models This book explores Microsoft’s Entity Framework and explains how it can used to build enterprise level applications. It will also teach you how you can work with RESTful Services and Google’s Protocol Buffers with Entity Framework and WCF. You will explore how to use Entity Framework with ASP.NET Web API and also how to consume the data exposed by Entity Framework from client applications of varying types, i.e., ASP.NET MVC, WPF and Silverlight. You will familiarize yourself with the new features and improvements introduced in Entity Framework including enhanced POCO support, template-based code generation, tooling consolidation and connection resiliency. By the end of the book, you will be able to successfully extend the new functionalities of Entity framework into your project.
Table of Contents (16 chapters)
Entity Framework Tutorial Second Edition
About the Author
About the Reviewers


The ADO.NET Entity Framework, the next generation of Microsoft's data access technology, is an extended Object Relational Mapping (ORM) technology that makes it easy to tie together the data in your database with the objects in your applications. This is done by abstracting the object model of an application from its relational or logical model. It is an extended ORM in the sense that it provides many additional features that a traditional ORM does not.

This book is a clear and concise guide to the ADO.NET Entity Framework. Packed with plentiful code examples, this book helps you learn the ADO.NET Entity Framework and ADO.NET Data Services and build a better data access layer for your application.

The intent of writing this book is updating you to the latest trends and developments as far as Entity Framework is concerned.

What this book covers

Chapter 1, Introducing the ADO.NET Entity Framework, is an introduction to the basics of the ADO.NET Entity Framework (EF), its usefulness, features, and benefits.

Chapter 2, Getting Started, discusses how you can get started with EF, create an Entity Data Model (EDM), and write a program to query data.

Chapter 3, Entities, Relationships, and the Entity Data Model, gives a detailed explanation of entities, relationships, and each of the sections of the EDM.

Chapter 4, Working with Stored Procedures in the Entity Data Model, provides a sample application that illustrates how to perform CRUD operations against the EDM.

Chapter 5, Working with Entity Client and Entity SQL, discusses the Entity SQL query language and how to work with the Entity Client provider.

Chapter 6, Working with LINQ to Entities, includes a detailed discussion on LINQ to Entities with many code examples.

Chapter 7, Working with the Object Services Layer, provides a detailed discussion on the Object Services Layer and its helpful and useful features.

Chapter 8, Working with WCF Data Services, provides an introduction to WCF Data Services, the REST architectural paradigm, and how these can be used with the EDM to perform CRUD operations.

Appendix, Advanced Concepts, covers a few advanced concepts. These include: REST and REST-based service frameworks and OData. We would also explore the HTTP methods and the request and response codes. Lastly, we would take a look at the new features in Entity Framework 7.

What you need for this book

To learn the concepts covered in this book, the reader should have a proper understanding and working knowledge of the following:



  • C#

To execute the code samples in this book, the following technologies should be installed on your system:

  • Visual Studio 2013 or later

  • SQL Server 2012 or later

  • Windows 7/Windows 8 or higher

Who this book is 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 do not need to know anything about the Entity Framework. Along the way, we will create some ASP.NET applications, so familiarity with this will be helpful. The book is a clear and concise guide to the ADO.NET Entity Framework 6 with plenty of real-life code examples.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Entity Framework 6 now provides support for spatial data types using DbGeography and DbGeometry types."

A block of code is set as follows:

public class User
        public virtual Int32 UserID { get; set; }
        public virtual String UserName { get; set; }
        public virtual String CreatedBy { get; set; }
        public virtual DateTime CreatedDate { get; set; }
        public virtual String ModifiedBy { get; set; }
        public virtual DateTime ModifiedDate { get; set; }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

static void Main(string[] args)
            DataContextDataContextvar context = new DataContext(GetConnectionString());

Any command-line input or output is written as follows:

edmgen /mode:fullgeneration /c:"Data Source=.;Initial Catalog=SecurityDB;User ID=sa;Password=sa1@3;" /p:SecurityDB

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can see that the Convert to Enum option is enabled."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.