Book Image

Code-First Development with Entity Framework

By : Sergey Barskiy
Book Image

Code-First Development with Entity Framework

By: Sergey Barskiy

Overview of this book

<p>Entity Framework Code-First enables developers to read and write data in a relational database system using C# or VB.NET. It is Microsoft's answer to demand for an ORM from .NET developers.</p> <p>This book will help you acquire the necessary skills to program your applications using Entity Framework. You will start with database configuration and learn how to write classes that define the database structure. You will see how LINQ can be used with Entity Framework to give you access to stored data. You will then learn how to use Entity Framework to persist information in a Relational Database Management System. You will also see how you can benefit from writing ORM-based .NET code. Finally, you will learn how Entity Framework can help you to solve database deployment problems using migrations.</p>
Table of Contents (15 chapters)
Code-First Development with Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Querying data in a database


In this section, we are going to look at our data using the query capabilities of Entity Framework. Typically, we will use LINQ to do this. We are going to start with a simple example though, accessing the data directly through DbSet. We will take a deeper look at LINQ in subsequent chapters. The code is quite simple and is as follows:

using (var context = new Context())
{
    var savedPeople = context.People;
}

If you set a breakpoint on the line with the last curly brace and look at the savedPeople variable in the Watch window, you will see one peculiar thing, something called Results View, shown in the following screenshot:

This illustrates an important concept. Entity Framework is using delayed query execution. In other words, the actual query command is sent to the database when the results of that LINQ query are accessed or enumerated. Entity Framework is doing so based on the IQueryable interface that DbSet implements. We can enumerate the results of our query...