Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

Introduction to Entity Framework Core

In the last module we took a look back at how Classic ASP and ASP.Net Web Forms performed data access using ADO and ADO.Net respectively.

Then we saw that starting with ASP.Net MVC, even back in .Net Full-Framework, Entity Framework became a very popular method of working with data. And this continues to be the case in ASP.Net Core MVC, Razor Pages, and even Blazor (not to mention desktop applications).

Starting in the next chapter, Chapter Three, “ASP.Net MVC Core – Models, Views, and Controllers”, we are going to make heavy use of Entity Framework Core. So it might be a good idea to get a quick overview here before we start building our Fred’s Cars web application in ASP.Net Core MVC.

Table Of Contents
  1. What is Entity Framework Core?
  2. The Model
    • Modeling the domain using C# POCOs
    • The DbContext
  3. Performing Queries using EF Core
  4. What's Next

What is Entity Framework Core?

Here is the definition Microsoft gives for EF Core (Entity Framework Core).

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.

The second part of their definition, however, denotes the more important factors for EF Core in my opinion:

EF Core can serve as an object-relational mapper (O/RM), which:

  • Enables .NET developers to work with a database using .NET objects.
  • Eliminates the need for most of the data-access code that typically needs to be written.

The above highlighted points tell us that we can use the EF Core ORM tool to map .Net C# objects to our database tables. And use C#, specifically Entity Framework Core (which is part of the CSharp language), to perform data-access rather than having to write complicated ADO.Net and Stored Procedures.

Finally, EF Core supports multiple Databases such as SQL Server, Oracle, and even Mongo (a No-SQL database system).

The Model

In the last module we took a look at the three components of MVC; the Model, the View, and the Controller. Here is the diagram we looked at again for review.

Entity Framework Core fits into the Model portion of an MVC Application.

Modeling the domain using C# POCOs

In order to perform data-access using Entity Framework Core, the first thing we would need to do is model our domain. For a local car dealership like Fred’s Cars, a C# object for a vehicle might look similar to the following.

Vehicle.cs

public class Vehicle
{
    public int Id { get; set; }
    public string Year { get; set; } 
    public string Make { get; set; } 
    public string Model { get; set; } 
    public string Color { get; set; } 
    [Column(TypeName = "decimal(18,2)")]
    public double Price { get; set; }
    public string VIN { get; set; } = string.Empty;
}

The above code creates a custom C# class that represents a Vehicle for the Fred’s Cars local dealership. We will learn all about creating classes like this with C# starting in the next chapter. But for now, this class just defines common properties of a Vehicle. Some of these properties are Make of type string and Price of type double. A string let’s us assign a series of characters strung together to the Vehicle’s Make property like, “Dodge”. And a double lets us assign a number with a decimal point to the Price property. We have also given the Vehicle class an Id property of type int (or integer). An int let’s us assign a whole number without a decimal point to the Id property of a Vehicle. This C# class called Vehicle will eventually map to a table in the database with the same name: Vehicle. And the Id property will be the unique Id for each individual vehicle in the table rows.

Usually there will be more than one C# model class in the model domain. For instance, there could be a VehicleType class that represents the vehicle category; car, truck, or jeep. But we will keep it simple here for the EF Core introduction.

The DbContext

Once we have defined our domain model, the next step is to create our application DbContext object. The DbContext class for the Fred’s Cars MVC application might look similar to the following.

ApplicationDbContext.cs

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options) 
    { }

    public DbSet<Vehicle> Vehicles => Set<Vehicle>();
}

In the code above, I’ve defined a C# class called ApplicationDbContext that inherits from DbContext. I don’t want to get into all the nitty gritty here. We will learn about setting up all of the plumbing needed for EF Core in the next chapter. But for now know that DbContext provides the functionality to create a session to the database for our web application to work with. And by our custom class inheriting from this “out-of-the-box” functionality, we will be able to communicate with the database.

The most important part of the code above is the following code line:

public DbSet<Vehicle> Vehicles => Set<Vehicle>();

The above line sets up a DbSet of Vehicles as a property of the ApplicationDbContext class. A DbSet is a set of Entities, in this case Vehicle Entities; hence the name Entity Framework. Each Vehicle entity represents a row in the database Vehicle table. And the Vehicles DbSet represents the Vehicle table itself.

Performing Queries using EF Core

One we have the Model defined, which includes the Vehicle C# POCO class and the ApplicationDbContext class, we can use the ApplicatonDbContext class to query the Database for Vehicles as shown in the code below.


using (var context = new ApplicationDbContext())
{
    var vehicles = context.Vehicles
        .Where(v => v.Make == "Dodge")
        .ToList();
}

In the code above I have created a variable called context and assigned to it an instance of the ApplicationDbContext class, the class that inherits from DbContext in EF Core. Next I define a variable called vehicles and assign to it a LINQ Query. In the LINQ query I start off from the DbContext and from there call the Vehicles property which is the Vehicles DbSet we defined in the Application DbContext. The Vehicles property by itself would return the entire list of Vehicles from the database. But from the Vehicles property we call a LINQ-to-Entities method called Where() and pass to it a lambda expression asking for only those vehicles whose Make property is equal to the string “Dodge”.

Again, do not worry too much about all of these new terms like LINQ, LINQ-to-Entities, method, and lamda. All of this will be explained in more detail when we actually put this into practice and build the car dealership in ASP.Net MVC.

What’s Next

In this module we took a whirl wind tour of what EF Core is really all about. And in this chapter we took a brief look at the historical context of .Net, .Net Core, ASP.Net, and ASP.Net Core so we can put everything into context going forward. I know how many times I’ve read a chapter like this and it just seemed tortuous because I could not wait to get my hands dirty and start building something. But, I tried to keep this chapter short and to the point.

The next chapter starts an exciting part of this book as we begin to really get into the ASP.Net Core Framework and building the Fred’s Cars web application in our first dynamic framework; ASP.Net MVC.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme