Member-only story
Creating Automatic Database Tables in an ASP.NET Core Project with Entity Framework Core
3 min readJan 5, 2025
To create database tables automatically in your ASP.NET Core project using Entity Framework Core (EF Core), you can use the Code First approach. Follow the steps below to accomplish this:
Installing EF Core Packages
Let’s install the necessary packages for using EF Core in your project:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
Defining the Context Class and Models
Creating a Model Class
Define a model class corresponding to your database table:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Stock { get; set; }
}
Defining the DbContext Class
Create a DbContext class to manage your model:
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
public…