Treehouse NewsLearn Entity Framework at Treehouse


writes on July 7, 2017

So you’ve mastered the basics of creating web pages and forms using Microsoft’s ASP.NET MVC web framework. What do you do next? Time to learn how to leverage the power of databases in your web apps!

What is Entity Framework?

For years, Microsoft’s ADO.NET—a data access technology provided by the .NET Framework—was the primary way that developers worked with data in relational databases. But developers often found themselves writing repetitive, boilerplate code containing string literals embedded with SQL statements and queries. Unfortunately, this approach prevents Visual Studio from being able to assist the developer with detecting syntax errors—even if it’s something as simple as a mistyped table or column name.

A database context class in Visual Studio

In contrast, Microsoft’s Entity Framework allows you to leverage the power of databases by writing nothing but C# code. You create a set of models—or entities—using plain old C# objects (POCO) classes and a database context class, and then use those classes to do database operations instead of writing SQL statements and queries.

Entity Framework—which is commonly referred to simply as “EF”—is a type of framework or library known as an Object Relational Mapper or ORM. EF is an essential part of the ASP.NET web stack.

Writing EF Queries

EF queries are written using C#’s Language Integrated Query—or LINQ—using one of two available syntaxes. The fluent syntax allows you to chain a series of method calls in order to compose a query whereas the query syntax uses a set of keywords to write SQL-like queries in C#. EF translates LINQ queries (known as LINQ to Entities) at runtime into the appropriate SQL statements so that they can be executed against a database.

To give you an idea as to how EF queries compare to SQL queries let’s look at a simple C# class method that returns a comic book record for the provided `comicBookId` parameter value. First, the implementation that uses ADO.NET and SQL:

And then the implementation that uses EF and a LINQ to Entities query:

When comparing the two examples, here are some things to note:

  • In the ADO.NET example, we’re explicitly creating a connection to the database using a SqlConnection object. In the EF example, the connection to the database is implicitly created for us.
  • In the ADO.NET example, our query is written using SQL embedded within a string literal. Doing this prevents Visual Studio from being able to assist us with the available table or table column names. Because of this, it’s very easy to reference a nonexistent table or column (do you see the intentional typo in our SQL query?) In the EF example, our query is written using LINQ. Because LINQ is just C# code, Visual Studio IntelliSense and the C# compiler will prevent us from referencing entities or entity properties that don’t exist.
  • Overall I find the look of the code in the EF example cleaner, more expressive, and simpler to read than the code in the ADO.NET example. Even if you’re unfamiliar with EF or LINQ queries, it’s hard to resist the conciseness and simplicity of the EF example.

In this simple example, you can see how EF can increase your productivity and make your data access code more reliable and easier to write.

EF Courses and Workshops

You’re ready to start using EF, but how do you get started? Well, with Treehouse of course!

Our EF courses start at the beginning and assume no prior knowledge. You’ll learn EF step-by-step by watching videos organized into sections called “stages”. Every time that you complete a stage, you’ll receive a badge. And you’ll reinforce your skills and knowledge by taking quizzes and completing in-browser code challenges.

We make it easy to learn EF with three courses and two workshops that cover the basics of EF and more.

Entity Framework Basics Course

This is where you’ll start your journey to learn EF.

In this course, you’ll learn how to use EF to persist application data to a database. By the end of the course, you’ll have learned how to use the Code First workflow, create entity classes, define relationships between entities, write LINQ queries, perform CRUD operations, and more.

Entity Framework Migrations Course

It’s not unusual for apps to evolve over time. These changes to your app will often include changes that you need to make to the database.

In this course, you’ll learn how to use EF Code First Migrations to propagate data model changes to your database. You’ll see how to enable migrations for a project, add and update migrations, modify a migration, and deploy migrations to dev/test/prod environments.

Entity Framework with ASP.NET MVC Course

In the EF Basics course, you’ll learn how to use EF to persist data to a database within the context of a console application. That helps to keep the focus on EF, but most of us likely build web apps, not console apps.

Entity Framework

The Comic Book Library Manager web app that you’ll work within the EF with ASP.NET MVC course.

In this course, you’ll learn how to use EF within the Comic Book Library Manager web app—a web application developed using the ASP.NET MVC web framework. You’ll work on completing this web app so that users can create, update, and delete comic books from their digital library.

Throughout this course, you’ll learn by example by completing hands-on coding exercises. See how to put EF related code into its own class library project, retrieve and persist data from within a web app, organize your data access code using popular design patterns, and more.

Entity Framework Data Concurrency Workshop

Data concurrency is the ability for multiple users to access or change data at the same time. The more users that you have trying to access and change the same data the more challenging it is to provide data that’s reliable and consistent. In this workshop, you’ll learn about data concurrency and how to implement optimistic data concurrency controls using EF.

Entity Framework Query Performance Workshop

It’s very common for every web app—at some point in its lifetime—to require some additional performance related tweaking or fine-tuning. In this workshop, you’ll learn how to audit EF queries and resolve common performance issues.

Get Started Today!

Get started with learning EF today! In addition to the EF related courses listed above, check out our ASP.NET MVC Basics and ASP.NET MVC Forms courses that’ll teach you how to create web pages and forms using ASP.NET MVC.

Not a Treehouse student? No problem! Start your 7-day free trial today.


Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Get Started

One Response to “Learn Entity Framework at Treehouse”

  1. Great goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic.

    I really like what you have acquired here, certainly like what you’re stating and
    the way in which you say it. You make it entertaining and you still take
    care of to keep it sensible. I can not wait to read far more from
    you. This is actually a terrific website.

Leave a Reply

You must be logged in to post a comment.

man working on his laptop

Are you ready to start learning?

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Start a Free Trial
woman working on her laptop