Complete Entity Framework Core Tutorial
Entity Framework Core (EF Core) is a lightweight and cross-platform version of the popular Entity Framework data access technology. It provides a powerful object-relational mapping (ORM) capability along with a simple yet powerful querying language.
Getting Started
To get started with EF Core, you first need to create a new .NET Core project. You can either use Visual Studio or the .NET Core CLI. In this tutorial, we will be using Visual Studio.
- Launch Visual Studio and create a new .NET Core project.
- Select “ASP.NET Core Web Application” and click on “Create”.
- Select the “API” template and click on “Create”.
- Once the project is created, install the following NuGet packages:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Tools
- Microsoft.EntityFrameworkCore.SqlServer
Connecting to a Database
EF Core supports different database providers including Microsoft SQL Server, MySQL, PostgreSQL, and SQLite. In this tutorial, we will be using SQL Server.
First, modify the “appsettings.json” file to include your database connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Next, add a new class named “ApplicationDbContext” which will inherit from “DbContext”:
public class ApplicationDbContext : DbContext
{
public DbSet<TodoItem> TodoItems { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Finally, add the following code to the “ConfigureServices” method in the “Startup.cs” file:
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connection));
Defining and Migrating Models
EF Core uses a feature called migrations to manage changes to your application’s data model. You create a migration each time you want to make changes to your model, and EF Core applies them to the database.
First, define your entity classes:
public class TodoItem
{
public int Id { get; set; }
public string Title { get; set; }
public bool IsDone { get; set; }
}
Next, create a migration by running the following command in the Package Manager Console:
add-migration InitialCreate
This will create a new migration file under the “Migrations” folder. You can modify the migration file to include any changes to your model.
Finally, apply the migration by running the following command:
update-database
This will apply the latest migration to the database and create the required tables.
Performing CRUD Operations
EF Core provides a simple API to perform CRUD operations on your entities.
The following code shows how to retrieve all “TodoItem” entities from the database:
using (var context = new ApplicationDbContext(options))
{
var todoItems = context.TodoItems.ToList();
}
The following code shows how to add a new “TodoItem” entity to the database:
using (var context = new ApplicationDbContext(options))
{
var todoItem = new TodoItem
{
Title = "Create a new tutorial",
IsDone = false
};
context.TodoItems.Add(todoItem);
context.SaveChanges();
}
The following code shows how to update an existing “TodoItem” entity:
using (var context = new ApplicationDbContext(options))
{
var todoItem = context.TodoItems.FirstOrDefault(t => t.Id == 1);
if (todoItem != null)
{
todoItem.Title = "Update an existing tutorial";
todoItem.IsDone = true;
context.SaveChanges();
}
}
The following code shows how to delete an existing “TodoItem” entity:
using (var context = new ApplicationDbContext(options))
{
var todoItem = context.TodoItems.FirstOrDefault(t => t.Id == 1);
if (todoItem != null)
{
context.TodoItems.Remove(todoItem);
context.SaveChanges();
}
}
FAQs
What is Entity Framework Core?
Entity Framework Core (EF Core) is a lightweight and cross-platform version of the popular Entity Framework data access technology. It provides a powerful object-relational mapping (ORM) capability along with a simple yet powerful querying language.
What databases does EF Core support?
EF Core supports different database providers including Microsoft SQL Server, MySQL, PostgreSQL, and SQLite.
What is a migration in EF Core?
EF Core uses a feature called migrations to manage changes to your application’s data model. You create a migration each time you want to make changes to your model, and EF Core applies them to the database.
How do I perform CRUD operations in EF Core?
EF Core provides a simple API to perform CRUD operations on your entities. You can retrieve, add, update, and delete entities using the API.