Learning Entity Framework Core
Entity Framework is a highly recommended Object Relation Mapping tool used to build complex systems. This course provides a full learning path to getting productive with the Entity Framework Core. Designed with the intermediate developer in mind and utilizing all of the latest features introduced by EF Core 2.0 and ASP.NET Core 2.0.
In this course, you will learn how to create, execute and rollback your own migrations. You will also get familiar with everything you need to create complex relational models. You will explore some of the most important advanced features of the entity framework like shadow properties, database generated values and raw SQL queries. You will know how to take an existing database and write a project based upon it with minimal intervention.
By the end of this course, you will certainly have added Entity Framework Core to your toolbox.
About The Author
Introduction to Entity Framework Core
This video provides an overview of the entire course.
Some of the viewers might not have any prior experiences with O/RMs so they might not know what an O/RM is.
• Understand O/RM
• Learn about EF Core
We need to work on a small starting project.
• Start a new ASP.NET Core 2.0 project
• Install Entity Framework Core along with its SQL Server provider using NuGet
• Add a connection string to appsettings.json
The Database context is vital to application development with Entity Framework Core. The viewers need to know more about it.
• Show an example of DbContext
• Create a new DbContext
• Add two DbSets to the newly created DbContext
The viewers have created a new project, installed EF Core and even created a DbContext but they haven’t yet seen a database.
• Register the DbContext with DI
• Create a static DbInit class
• Get an instance of the DbContext and call EnsureCreated
The viewers would want to view the fake data inserted in the database. The SQL Server Object Explorer is a very helpful tool for that job.
• Pull up the SQL Server Object Explorer
• Use the View Data feature
• Drop the database
Now that you are done with the videos of section 1, let’s assess your learning. Here, are a few questions; followed by 4 options, out of which 1 is the correct option. Select the right option and validate your learning!
Create, Execute, and Roll Back Migrations
Viewers without prior exposure to O/RMs have no knowledge of what an O/RM is.
• Provide a detailed explanation of what migrations are
• Describe the advantages of using migrations
Following a theoretical introduction the viewers will need to know how to create their own migrations.
• Add entities to the project
• Add a DbContext
• Execute the migrations and update the database
In order to demonstrate one of the key benefits of using migrations we need to attempt to update a table.
• Make the change in the model
• Create a new migration
• Execute the migration and inspect the change in SQL Server Object Explorer
After having created and updated a table the viewers will want to know how they can go a step back.
• Make an intentional mistake during the creation of an entity
• Apply that migration
• Roll back to a correct state
In the videos so far we’ve used the PMC to work with migrations. Migrations are still possible with the command line.
• Create a new project add entities and a DbContext
• Guide the viewer through the installation of the required packages
• Demonstrate the basic migration operations in the CMD
Now that you are done with the videos of section 1, let’s assess your learning. Here, are a few questions, followed by 4 options, out of which 1 is the correct option. Select the right option and validate your learning!
Create Models and Connect Them Through Relationships
You need to start off with something simple like restricting max length.
• Use the MaxLength attribute in a model
• Describe what changes in the migration when we use [MaxLength]
In this video, you’ll get introduced to the concept of primary keys with EF Core.
• Set a primary key by convention
• Set a Primary Key using the Fluent API
Unique fields that are not primary keys are `a fairly common trait in database oriented applications.
• Create a unique constraint using alternate keys
• Create a unique constraint using indexes
• Understand about the indexes
Sometimes there are properties that you need in the model but not in the database.
• Use the [NotMapped] attribute indicate that the field should not be included
Work with default and database generated values.
• Create a default value for a field using the Fluent API
• Mark that value as database generated
• Learn about Database Generated attribute does not configure generation on its own
Introduce the viewer to relationships in general and one-to-one relationships in particular.
• Give a brief talk on relationships
• Establish a one-to-one relationship using conventions
• Do the same with the Fluent API
Work with one-to-many relationships.
• Use conventions to establish a one-to-many relationship
• Pave the way for the next video on many-to-many relationships
Demonstrate how to use two one-to-many relationships to establish a many-to-many relationship.
• Create an entity for the junction table
• Demonstrate the creation of a one-to-many relationship
• Move to a many-to-many relationship
Now that you are done with the videos of section 3, let’s assess your learning. Here, are a few questions, followed by 4 options, out of which 1 is the correct option. Select the right option and validate your learning!
Performing Querying Operations
The first obvious type of query we need to deal with is the read query of course.
• Read all rows from a table
• Use SingleOrDefaultAsync to load a single row
Some queries, especially the more complex ones can be done in LINQ too.
• Use LINQ queries to perform the same operations as in the previous vide
Having worked with reading operations, it is now time to work with create, update and delete operations.
• Write a new row to a DB table
• Update a row in the table
• Delete a row using the primary key
Loading strategies are important for loading related data through navigation properties.
• Learn about the need for loading strategies
• Show examples of explicit loading with Reference and Collection
• Mention that Lazy Loading is not yet supported by EF Core
Eager loading is the most efficient way to pre-load related data.
• Perform the previous video’s operations using Eager Loading this time
Query Objects are a powerful way to make complex queries without having to maintain complex methods with long names.
• Mention Services and Repositories but do not focus on them
• Implement a query object
Now that you are done with the videos of section 4, let’s assess your learning. Here, are a few questions, followed by 4 options, out of which 1 is the correct option. Select the right option and validate your learning!
Advanced EF Core
Entity Framework Core does not come with a built-in seeding mechanism and seeding is a very common practice in database systems.
• Create a class with extension methods for AppDbContext
• Load some data in a few tables
• Seed the database from within Startup.cs
Although Entity Framework Core provides transactions right out of the box with DbContext, sometimes we need to take full control.
• Learn about the SaveChanges and SaveChangesAsync works transactionally
• Execute a simple transaction
We need to present raw SQL queries.
• Execute a simple raw SQL query
• Create a stored procedure with Visual Studio
• Execute the stored procedure with raw SQL
When multiple users attempt to edit a single entity, concurrency issues arise.
• Identify the problem
• Solve the problem with a rowversion column
Sometimes it's not possible to start with a code first approach.
• Create a new database using the SQL Server Object Explorer
• Install EF Core
• Scaffold models and DbContext
Now that you are done with the videos of section 5, let’s assess your learning. Here, are a few questions, followed by 5options, out of which 1 is the correct option. Select the right option and validate your learning!