0. Introduction - Start here

If you want to learn the basics of Entity Framework, but use best design and architectural patterns, start from this page. GitHub Repository: https://github.com/saqibrazzaq/efcorebeginner You will learn how to do CRUD operations on a single table with EF Core (using SQL Server as the backend database). In this beginner series, we will keep just one table with 4-5 fields. You will learn to Create entity classes, which represents database schema Use Entity Framework DbContext to represent the entities Code first approach, design classes first, update database later Use repository pattern to keep the EF Core related stuff together Use service classes for business logic and domain classes Create controllers to expose web APIs Create DTOs for API clients Do transformation between DTOs and entity classes Validate data in service layer Better and generalized exception handling Search, sort and paging using EF Core LINQ You can learn and practice Entity Framework by just initializing DbContext with database connection string, then create DbSet and do CRUD operations....

4 min · 847 words · Saqib Razzaq

1. Single table basic example

So start by creating a new project in Visual Studio. At this time the latest version of Visual Studio is 2022 and I am using the Community Edition, which is free. .NET Framework Core 7 just released, so we will use the latest versions. We will create a basic example with single table named Person. Person table will have few basic columns like name, gender and phone number. New ASP.NET Core Web API project in Visual Studio 2022 Choose all the default options in the next step....

4 min · 738 words · Saqib Razzaq

2. Generate database and tables

This is the second article of the introduction to EF Core series. In the previous article we Created a new ASP.NET Core Web API project Added an entity class Person Added AppDbContext class to use Entity Framework In this article we will initialize the database context class and generate the database tables. Initialize database context There is a file Program.cs in every ASP.NET Core Web API project. This file is loaded when the project starts execution....

5 min · 1065 words · Saqib Razzaq

3. Generic Repository Base Class

This is the 3rd article on the beginning EF Core series. In the previous articles we created a new Web API project, entity class, DbContext class, used DotNetEnv to keep connection string in env file, initialized the EF Core in Program.cs and then finally created the database using our first migration command. Photo by Pixabay: https://www.pexels.com/photo/asphalt-balance-blur-close-up-268018/ Now it is time to do CRUD operations with the database using the Entity Framework....

5 min · 1043 words · Saqib Razzaq

4. Repository class for Person Entity

You are reading the 4th article in the beginning EF Core series with ASP.NET Web API. So far we have setup the base repository class that uses the Entity Framework’s DbContext and deals with DbSet. RepositoryBase creates DbSet, which is very generic. One generic class cannot handle all operations done on an entity. So we create separate Repository classes for each entity. That means if we have 10 tables, we will have 10 Entities and 10 Repository classes....

2 min · 343 words · Saqib Razzaq

5. Transactions with RepositoryManager

This is the 5th article in the EF Core beginner series. So far we created base repository classes which uses Entity Framework’s DbContext to do CRUD operations. In this article, you will learn how to add support for transactions and how to get reference to all the repositories. IRepositoryManager Interface Why we need another generic interface? We created IRepositoryBase, which is the base class for all the repositories. Then we created IPersonRepository, it inherited from IRepositoryBase....

3 min · 538 words · Saqib Razzaq

6. Use Repository in Service, and DTOs

You are now reading the 6th article in the beginner series of EF Core beginner series. In the previous articles we created the RepositoryManager for getting any repository. We also worked on the RepositoryBase class that handles CRUD operations on entities using EF Core’s DbContext class. Now we are finally ready to use the Repository in our service classes. IPersonService interface Add a new folder Services in the project. In this folder add a new interface IPersonService....

8 min · 1515 words · Saqib Razzaq

7. Controller to create new Person

You are reading the 6th article in the beginner series of the Entity Framework Core with ASP.NET web API. We are using best practices to write .NET web API using EF Core. Since it is a beginner series, we are only using single table to keep things simple. In the last article we created service to manage Persons. The service deals with the repositories. The service uses DTOs to send/receive data from the client/controller....

5 min · 927 words · Saqib Razzaq

8. Update, Delete and List all Persons

You are now reading the 8th article in the beginning of EF Core series. In the last article, we created a new controller for Person and added a method to handle HttpPost request to create a new person. In this article you will learn how to do the rest of the CRUD, which are update, delete and Read all records. List All Persons To list all persons, we need to update two methods....

10 min · 2001 words · Saqib Razzaq

9. Searching, paging and sorting in Entity Framework 7

This is the 9th article in the beginning series of the Entity Framework Core tutorial. In the previous articles, we built base classes for repository and built services and controllers to do simple CRUD operations on Person table. Searching, paging and sorting are more complex than basic Insert, Update and Delete methods. But it is very common to use in any project. Any project that has more than 20 records in a table, must implement searching, paging and sorting....

7 min · 1462 words · Saqib Razzaq