Entity Framework in Depth

Learn to query or save domain objects to a database using Entity Framework
4.6 (943 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
4,057 students enrolled
$19
$50
62% off
Take This Course
  • Lectures 79
  • Length 6.5 hours
  • Skill Level Intermediate Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 10/2015 English

Course Description

Entity Framework is an Object / Relational Mapper (O/RM) that helps you read and write data from and to a database. In this course, Mosh, teaches you the core concepts of Entity Framework through a series of clear, concise and hands-on lectures.


Whether you're new to Entity Framework, or been using it for a while but are looking for a comprehensive course with a clean structure to fill the missing parts, you're going to love this course.


  • Understand the differences between code-first and database-first workflows
  • Build a domain model using database-first workflow
  • Build a domain model using code-first workflow (with a new or an existing database)
  • Use code-first migrations to upgrade or downgrade your database
  • Override code-first conventions (using data annotations and fluent API)
  • Query data using LINQ (using query syntax and extension methods)
  • Use eager loading, lazy loading and explicit loading effectively
  • Add, update and remove objects
  • Use additional tools to profile and troubleshoot your applications
  • Understand what the repository pattern is, why and when to use it and how
  • Apply best practices with Entity Framework


Unlike other books or courses that teach Entity Framework like a manual, Mosh walks you through real-world scenarios using his extensive experience in the field. Not only does he teach you the fundamentals of Entity Framework, he'll also teach you best practices along the way.


In each section, you'll find exercises to help you reinforce the materials. These exercises have been chosen from real-world scenarios to help you prepare for the challenges of the real-world. Also, some sections include cheat sheets to you can quickly review what you learn in that section.


This course is produced with Entity Framework 6 and will be updated as new versions of Entity Framework come out. If you're using an older version of Entity Framework, you can still take this course as long as your version of Entity Framework is 4.1 or higher.


What others say

"May God bless you and your efforts to create high quality courses. This has really changed my life from working as a security guard and doing websites for free, now I am a professional." -Danish Jafri


"I'm impressed by Mosh's ability to strike the right balance of topic background, demo and tip and tricks to get around common issues when learning a technology. Great job Mosh! I'm more than happy to have invested my time and money in your work." -Ben Holtzman


"Excellent course! Mosh has a brilliant style and all of his courses are well presented and very high quality. This course was no exception and I learnt a lot from it." -Steven Barnett


"It´s a pleasure following the lessons, It´s like a good book, you always want to read another chapter. This is not siimple a course of EF, it goes further, gives a very good explanation of Linq and the most brilliant description of Repository Pattern and Unity of work. Please keep doing courses. To be honest I believe that you should leave your work and spend the rest of your life teaching ;)." -Martin Suarez


"I'd taken a lot of video classes, and this is the best I'd taken. Very well explained, detailed and a good real life examples, lots of tips and more." -Oscar Vargas


"As always... Mosh nails it with his thorough explanations (without all the "I'm better than you vibe")." -Mike Loffland


100% money back guarantee

This course comes with a 30-day money back guarantee. If you're not happy, ask for a refund, all your money back, no questions asked.


Do you want to take your programming skills to the next level?

Then take the course now and let's get started.


What are the requirements?

  • Good understanding of advanced C# constructs (generics, lambda expressions)

What am I going to get from this course?

  • Understand the differences between database-first and code-first workflows
  • Build a domain model using database-first workflow
  • Build a domain model using code-first workflow (with an existing or a new database)
  • Use code-first migrations to upgrade or downgrade your database
  • Override code-first conventions (using data annotations and fluent API)
  • Query data using LINQ (using query syntax and extension methods)
  • Use eager loading, lazy loading and explicit loading effectively
  • Add, update and remove objects
  • Use additional tools to profile and troubleshoot your applications
  • Understand what the repository pattern is, what and if you need it, and how to implement it
  • Apply best practices with Entity Framework

What is the target audience?

  • Any .NET developer who wants to persist or query domain objects from a database

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction
About this Course
Preview
01:00
About Me
Preview
01:35
Asking Questions
02:58
Section 2: Getting Started
Introduction
Preview
00:26
What is Entity Framework?
Preview
01:07
Workflows
Preview
01:33
Demo: Database-First Workflow
Preview
10:22
Demo: Code-First Workflow
09:07
Database-First or Code-First?
03:41
Summary
00:24
Section 3: Building a Model using Database-First Workflow
Introduction
00:35
00:54

In this lecture, we'll create a database using a SQL script. This database will be used throughout this section to build a model using database-first workflow.

This lecture contains downloadable script in the Supplementary Materials section.

06:46

In this lecture, you'll learn some handy features of the EDMX designer that make it easy for you to work with large, complex models. You'll learn how to:

  • Zoom in and out of your model
  • Export a diagram as image
  • Collapse or expand all entities
  • Move entities around, or have them automatically laid out in a clean way
  • Show or hide data type of properties
  • Rename entities and their members
06:56

In this lecture, you'll see the XML behind an EDMX model. I'll show you the conceptual model, storage model, and the mapping between the two, both in the XML and Model Browser.

You'll also learn how to modify the mapping between the storage model and the conceptual model.

03:16

In this lecture, I'll deconstruct the connection strings with database-first models and explain what CSDL, SSDL and MSL resources mean.

13:30

In this lecture, I'll take you through a few different scenarios when you modify your database. I'll show you how to keep your model in sync with the database when you:

  • Add a new table
  • Modify an existing table (including renaming columns, changing their data types, adding / removing columns)
  • Delete a table


05:28

In this lecture, you'll learn how to import stored procedures and functions into your entity data model.

07:38

In this lecture, we'll take a closer look at the Function Import dialog box. You'll learn how to deal with legacy databases with poorly named functions / stored procedures and prevent such pollutions to affect your model. I'll also introduce you to complex types, what they are, when they are useful, and how to create them.

07:22

In this lecture, you're going to learn how to use enums in your entity data model to improve the readability and maintainability of your code. I'll teach you how to define new enums in your model, or reference an existing enum in your project.

Exercises
3 pages
Summary
00:17
Section 4: Building a Model using Code-First Workflow
Introduction
00:23
13:15

In this lecture, you'll learn how to build a more complex model (with a few classes and different types of associations between them) using the code-first workflow. More specifically, you'll see one-to-many and many-to-many relationships.

06:58

A common misconception about the code-first workflow is that it is only suitable for greenfield projects without an existing database. In this lecture, I'm going to show you how to use the code-first workflow against an existing database.

04:21

In this lecture, I'm going to show you a common issue that many beginners face when enabling migrations on an existing database. I'll introduce you to -IgnoreChanges and -Force switches of Add-Migration command and how they help you create the initial migration against an existing database.

09:29

In this lecture, you'll learn one migration scenario: adding a new class to your model and bringing your database in sync with your model. I'll also teach you how to populate the corresponding table using code. This is useful in scenarios where your tables have reference data that need to be automatically populated as part of running migrations.

08:25

In this lecture, I'll walk you three modification scenarios:

  • Adding a new property to a class
  • Modifying an existing property
  • Deleting an existing property
Then, I'll show you how sometimes migrations can cause data loss and how to prevent this.
05:53

In this lecture, you'll learn how to bring your database in sync with your model when you delete an existing class. I'll also teach you how to preserve data in your database for historical reasons.

01:22

What if one of your past migrations was a mistake? What if you shouldn't have made that change on your model? This is a scenario that happens occasionally in the real-world and many beginners don't know how to deal with these situations. In this lecture, I'll explain how to recover from your mistakes in a safe way.

06:01

There are times that you need to restore your model and database to a previous state, perhaps for maintaining an older version of your application. In this lecture, I'll present you with two strategies for doing this.

Seeding Database
06:27
Exercises
2 pages
Summary
00:27
Section 5: Overriding Code-First Conventions
Introduction
00:25
08:17

In this lecture, I'll introduce you to two ways to override code-first conventions: Data Annotations and Fluent API. I'll show you an example of each approach, and then compare them side-by-side.

07:17

In this lecture, we'll have a closer look at various data annotations to override code-first conventions. You'll learn how to override:

  • The name of tables and their schema
  • The name of columns and their type
  • Primary keys (including composite keys)
  • Null values
  • Length of strings
  • Foreign keys
  • Indices
08:04

In this lecture, you'll put what you learned in the last lecture in action. We'll apply a few data annotations on our model and see the effect of these annotations in the database. This lecture contains downloadable project, so you can follow along with me.

03:45

In this lecture, you'll learn how to use Fluent API to override:

  • Table names and their schemas
  • Primary keys (including composite keys)
  • Identity columns
  • Column types
  • Column types
  • Order of columns
  • Null columns
  • Length of strings
10:05

Here, I'll show you how to configure various kinds of relationships with Fluent API:

  • One-to-many
  • Many-to-many
  • One-to-zero/one
  • One-to-one

You'll also learn about Principal and Dependent in Entity Framework.

13:28

In this lecture, you'll put what you've learned about Fluent API in action. This lecture includes downloadable code so you can code along with me. We'll use Fluent API to:

  • Make columns NOT NULL
  • Set a length to string columns
  • Change the name of foreign keys
  • Turn off cascade delete on relationships
04:03

In this lecture, you'll learn how to use Fluent API to override the name of foreign key columns in an intermediary table in a many-to-many relationship.

07:37

As you work with larger and more complex models, organising your Fluent API configurations becomes more important. In this lecture, I'll share with you my personal convention to organize your Fluent API configurations, which will incredibly simplify the maintenance of data access layer of your application.

Exercises
3 pages
Summary
00:36
Section 6: Querying Data using LINQ
Introduction
00:18
03:19

Here, I'll give you a brief background about LINQ. I'll explain what we had to do before LINQ and how LINQ makes our job much easier.

07:49

In this lecture, I'll show you how to use LINQ to get data out of a database. You'll get familiar with two ways to use LINQ:

  • Using LINQ syntax
  • Using LINQ extension methods
17:00

In this lecture, we'll have a closer look at the LINQ syntax. You'll learn how to:

  • Filter data
  • Sort data
  • Project data (into other types or anonymous types)
  • Group records
  • Use joins (inner join, group join and cross join)
16:09

In this lecture, I'll teach you how to use LINQ extension methods to:

  • Filter data
  • Sort data (including multiple columns)
  • Project data (using Select and SelectMany)
  • Group records (using GroupBy)
  • Join records (e.g. inner join, group join and cross join)
05:41

In this video, you'll learn the additional LINQ extension methods that are not available in LINQ syntax. You'll learn how to:

  • Partition data (e.g. returning records for a given page)
  • Return unique records
  • Use aggregate functions (e.g. Count, Max, Min, Sum)
  • Check to see if all records match a given criteria
  • Check to see if there are any records that match a given criteria
  • Return single records (e.g. First, Last, Single, etc)
07:11

In this lecture, you'll learn about deferred execution and its benefits. I'll also show you how to break this behaviour and immediately execute your queries, which is sometimes useful if your query expressions cannot be translated into SQL. I'll also introduce you to SQL Server Profiler for inspecting queries run on a database.

07:40

In this lecture, we'll dig deep into the IQueryable interface. I'll explain what it is and how it is different from IEnumerable. I'll also you introduce you to the differences between Func<> and Expression<> and how the latter allows extending queries.

Exercises
3 pages
Summary
00:37
Section 7: Loading Related Objects
Introduction
00:14
08:05

In this lecture, you'll learn what lazy loading is, how it works under the hood and when to use / avoid it.

02:59

In this lecture, I'll introduce you to N + 1 problem that sometimes comes in technical interviews. I'll show you how lazy loading, if misused, can have an adverse impact on the performance of your application by causing unnecessary round-trips to the database.

06:00

In this lecture, you'll learn what eager loading is and how to use it. I'll introduce you to the Include() method and show you two ways of using it: the poor way, and the good way.

08:55

In this lecture, you'll learn how to use explicit loading to simplify complex queries that are result of too much eager loading. You'll also learn about another benefit of explicit loading: being able to apply filters when loading related objects.

Exercises
1 page
Summary
00:43
Section 8: Updating Data
Introduction
00:29
01:23

In this lecture, I'll show you what happens inside DbContext as we add, modify and remove objects.

07:49

In this lecture, you'll learn how to add objects using Entity Framework. I'll show you three ways to create association between your objects and explain which approach works better in WPF or ASP.NET MVC applications:

  • Using an existing object in a context
  • Using foreign key properties
  • Using the Attach method
02:22

In this lecture, you'll learn how to update objects with Entity Framework.

06:44

In this lecture, you'll learn how to remove one or a range of objects from your database. I'll show you two examples, one with CASCADE DELETE enabled, and another without it.

06:03

In this lecture, I'll show you how to get the state of your objects from DbContext. This can be useful when troubleshooting your applications and you want to know exactly what is happening inside a DbContext. Also, if you want to implement an audit trail, you can get these of these objects and dump them into an audit trail.

07:27

In this lecture, I'll introduce you to a very useful tool when working with LINQ and Entity Framework: LINQPad. I'll give you a quick tour of LINQPad and how easily you can run your queries to get immediate feedback about the return values and SQL statements executed on the database.

Exercises
1 page
Summary
00:20
Section 9: Using Entity Framework in Your Applications
Introduction
00:33
11:42

In this lecture, I'll explain what repository pattern is and what it is not, its benefits, why and if you need, and address some of the misconceptions about repositories. Some developers argue that the repository pattern is already implemented in Entity Framework and there is no need to re-implement it. I'll explain why this argument is not correct with real-world examples.

14:35

In this lecture, I'll walk you step-by-step through implementing the repository and unit of work patterns with Entity Framework. I'll show you the interfaces and their implementation. You can download the solution in the Supplementary Materials of this lecture.

04:09

In this lecture, I'll give you a recipe to structure your applications. I'll show what classes go in each layer. You can download the solution in the Supplementary Materials of this lecture.

07:42

In this lecture, you'll see an example of Entity Framework in an ASP.NET MVC application. I'll show you how to display the list of courses on the home page and add a new course to the database. You'll learn two ways to work with contexts: the simple way by directly initialising a private field in the constructor, and a more advanced way, using dependency injection.

06:14

In this lecture, you'll see an example of Entity Framework in a WPF application. I'll show you how to bind a data grid to a list of courses and add a new course to the list.

Summary
01:01
Section 10: Conclusion
Final Words
01:01
Get My Other Courses with a Discount
Article

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Mosh Hamedani, Passionate Software Engineer and Best-selling Author

Moshfegh Hamedani is a passionate and creative software engineer with a strong focus on pragmatism and simplicity. He started programming at the age of seven on a Commodore 64. Later, during his high school years, he started learning C and C++ . In 2002, along with the first release of .NET, he shifted his focus to C#. Since then he has been involved in the design and implementation of numerous software projects, including modern web applications, mobile apps, desktop applications and frameworks.

Aside from his career as a software engineer, he truly enjoys sharing his knowledge with others. Since his early twenties, he has been teaching courses and running workshops on C#, ASP. NET, object-oriented programming and clean coding. He is author of 6 best-selling courses on Udemy and more will be coming soon.

Mosh has a Master of Science in Network Systems and a Bachelor of Science in Software Engineering. He is also a Microsoft Certified Application Developer, Technology Specialist (Web Applications) and Professional.

Outside the software world, Mosh is a photographer, a pianist and a passionate Latin dancer.

Ready to start learning?
Take This Course