Entity Framework in Depth: The Complete Guide
With over 6,500 students and 1,500+ positive reviews, this course is Udemy’s most popular course for learning Entity Framework!
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.
By the end of watching this course, you’ll be able to:
- 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 so you can quickly review what you learn in that section.
This course is produced with Entity Framework 6. 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.
In order to take this course, you should have at least 3 months experience programming in C#. If you need to strengthen your C# fundamentals, you can take Mosh’s related courses:
- C# Basics for Beginners
- C# Intermediate: Classes, Interfaces and Object-oriented Programming
- C# Advanced
WHAT OTHER STUDENTS WHO HAVE TAKEN THIS COURSE 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 GAUARANTEE
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?
Join the other 6,500 happy students who have taken this course and start coding within minutes.
Building a Model using Database-First Workflow
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.
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
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.
In this lecture, I'll deconstruct the connection strings with database-first models and explain what CSDL, SSDL and MSL resources mean.
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
In this lecture, you'll learn how to import stored procedures and functions into your entity data model.
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.
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.
Building a Model using Code-First Workflow
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.
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.
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.
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.
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
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.
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.
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.
Overriding Code-First Conventions
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.
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
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.
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
Here, I'll show you how to configure various kinds of relationships with Fluent API:
You'll also learn about Principal and Dependent in Entity Framework.
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
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.
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.
Querying Data using LINQ
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.
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
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)
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)
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)
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.
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.
Loading Related Objects
In this lecture, you'll learn what lazy loading is, how it works under the hood and when to use / avoid it.
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.
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.
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.
In this lecture, I'll show you what happens inside DbContext as we add, modify and remove objects.
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
In this lecture, you'll learn how to update objects with Entity Framework.
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.
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.
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.
Using Entity Framework in Your Applications
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.
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.
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.
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.
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.