---
title: "What is DB first approach in Entity Framework core?"  
description: "What is DB first approach in Entity Framework core?"  
author: "Revati S Misra"  
published: 2024-01-15  
updated: 2024-01-17  
canonical: https://www.mindstick.com/forum/160563/what-is-db-first-approach-in-entity-framework-core  
category: ".net core"  
tags: ["asp.net", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# What is DB first approach in Entity Framework core?

What is DB first approach in [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach)?

## Replies

### Reply by Manish Sharma

The [DB first approach](https://www.mindstick.com/articles/1551/crud-operations-in-mvc-using-db-first-approach) in **[Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) Core** is a development approach that involves creating the entity data model from an existing database. This approach allows developers to generate **entity classes**, **context classes**, and **configuration** files based on the **structure and schema** of an **existing database.** It is particularly useful when working with legacy databases or when the database design is already established. With the [DB first approach](https://www.mindstick.com/articles/1551/crud-operations-in-mvc-using-db-first-approach), developers can easily map database tables to entity classes and start building applications without having to manually define the data model.

Here's an example of using the **DB first approach** in Entity Framework Core:

1. First, you would have an **existing database** with **tables**, **columns**, and **relationships**.
2. Next, you would use the **Entity Framework Core tools** to generate entity classes, context classes, and configuration files based on the existing database. You can do this using the **Scaffold-DbContext command** in the **Package Manager Console or the .NET Core CLI.**

For **example**, using the Package Manager Console:

```plaintext
Scaffold-DbContext "Server=YourServer;Database=YourDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
```

1. This command will **create entity classes** based on the tables in your database and a context class that represents the database context.

**After running the Scaffold-DbContext command,** you will have generated entity classes that represent the tables in your database. You can then use these entity classes to query and manipulate data in your database using Entity Framework Core.

Here's an example of a generated entity class:

```plaintext
public class Product
{
   public int ProductId { get; set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
}
```

With these generated entity classes, you can start building your application using **Entity Framework Core** to interact with the existing database.


---

Original Source: https://www.mindstick.com/forum/160563/what-is-db-first-approach-in-entity-framework-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
