Book Image

Mastering Entity Framework

By : Rahul Rajat Singh
Book Image

Mastering Entity Framework

By: Rahul Rajat Singh

Overview of this book

<p>Data access is an integral part of any software application. Entity Framework provides a model-based system that makes data access effortless for developers by freeing you from writing similar data access code for all of your domain models.</p> <p>Mastering Entity Framework provides you with a range of options when developing a data-oriented application. You’ll get started by managing the database relationships as Entity relationships and perform domain modeling using Entity Framework. You will then explore how you can reuse data access layer code such as stored procedures and table-valued functions, and perform various typical activities such as validations and error handling. You’ll learn how to retrieve data by querying the Entity Data Model and understand how to use LINQ to Entities and Entity SQL to query the Entity Data Model.</p>
Table of Contents (19 chapters)
Mastering Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Software developers using .NET technologies have been using ADO.NET for data access for more than a decade. It was with the release of .NET Framework 3.5 that Microsoft provided one more data access technology—Entity Framework. Entity Framework is an Object Relational Mapper (ORM) that sits on top of ADO.NET. Entity Framework lets the developer write the data access code in terms of models rather than SQL queries, which makes creating the data access layer much simpler and easier.

This book is for the .NET developers who develop data-driven applications using ADO.NET or other data access technologies. This book will give them everything that is required to effectively develop and manage data-driven applications using Entity Framework. We will learn the various approaches that Entity Framework provides us and which approach should be used for which scenario. We will learn how to perform domain modeling, validations, transaction and concurrency handling using Entity Framework.

We will also take a look at the various ways of querying the data using Entity Framework. We will see how we can reuse database procedures using Entity Framework. We will also take a look at passing dynamic queries from the application using Entity Framework. This book will help developers build all the skills required to effectively develop and manage data-driven applications using Entity Framework.

What this book covers

Chapter 1, Introduction to Entity Framework, introduces the user to Entity Framework. We will show how a data-centric application can benefit from the Database First approach and a domain-centric application can benefit from a Model First or Code First approach.

Chapter 2, Entity Framework DB First – Managing Entity Relationships, discusses how relationships can be managed using Entity Framework (one-to-one, one-to-many, and many-to-many).

Chapter 3, Entity Framework DB First – Performing Model Validations, discusses how model validations can be performed using the Entity Framework provided partial methods.

Chapter 4, Entity Framework DB First – Inheritance Relationships between Entities, discusses the Table per Type, Table per Hierarchy, and Table per Concrete Class inheritance relationships in Entity Framework.

Chapter 5, Entity Framework DB First – Using Views, Stored Procedures, and Functions, discusses how we can use Entity Framework with stored procedures and functions.

Chapter 6, Entity Framework Code First – Domain Modeling and Managing Entity Relationships, discusses how we can map the domain entities to the database tables. We will learn how to manage the relationships between models by using the Code First conventions and configurations.

Chapter 7, Entity Framework Code First – Managing Database Creation and Seeding Data, discusses how we can manage the database creation process and insert some dummy data into the database.

Chapter 8, Querying the Entity Data Model – LINQ to Entities, discusses how we can query the object model using LINQ to Entities.

Chapter 9, Querying the Object Model – Entity SQL, discusses how we can query the object model using Entity SQL

Chapter 10, Managing Concurrency Using Entity Framework, covers various concurrency management techniques using Entity Framework.

Chapter 11, Managing Transactions Using Entity Framework, discusses how to manage transactions using Entity Framework.

Chapter 12, Implementing a Small Blogging Platform Using Entity Framework, introduces a complete sample application using the Entity Framework Code First approach to demonstrate all the concepts in action.

What you need for this book

The following software is required to complete the practice exercises:

  • Windows 7 or higher

  • SQL Server Developer Edition or Express edition

  • Visual Studio 2010 or higher

  • .NET Framework 4.0 or higher

  • Entity Framework 4.0 or higher

Who this book is for

This book is for .NET developers who develop data-driven applications using ADO.NET or other data access technologies. This book will give you everything you need to effectively develop and manage data-driven applications using Entity Framework.

Conventions

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: "Using IQueryable, gives you a chance to create a complex LINQ query using multiple statements without executing the query at the database level."

A block of code is set as follows:

using(SampleDbEntities db = new SampleDbEntities())
{
   IEnumerable<Employee> employees = db.Employees
   .Where(employee => employee.LastName == "Singh");
}

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

public partial class Role
{
    public Role()
    {
        Users = new HashSet<User>();
    }

    public int Id { get; set; }

    [Required]
    [StringLength(256)]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

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: "Using this approach will check for concurrency issues for only those fields that are marked with Concurrency Mode as Fixed."

Note

Warnings or important notes appear in a box like this.

Tip

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 www.packtpub.com/authors.

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 http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

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 http://www.packtpub.com/submit-errata, 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 https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

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.

Questions

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.