Member-only story

Creating Automatic Database Tables in an ASP.NET Core Project with Entity Framework Core

Onur Karaoz
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…

--

--

Onur Karaoz
Onur Karaoz

Written by Onur Karaoz

As a .NET Developer, I have experience in JavaScript, ASP.NET Core, and API development. I also share my knowledge and experiences in the software world.

No responses yet