Site icon Treehouse Blog

Learn Entity Framework at Treehouse

Entity Framework - Object Relational Mapper

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 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.

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.

Exit mobile version