How to convert Database first to code first in ASP.NET Core

Sometimes you have a database and you want to convert it to Code First. Or maybe creating a database by Code First is boring and time-consuming. you can create your database with its relations in SQL Server, then convert it into Code First. For doing this, please follow these steps:

  1. Open Visual Studio and create a Web Application:

Select the kind of your project. It depends on your project:

  1. Open Package Manager Console and install below packages:
Install-Package Microsoft.EntityFrameworkCore.Tools

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Then run below command in your Package Manager Console

Scaffold-DbContext "Data Source=.;Initial Catalog=Test; Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

In which Data Source is a connection string that can be different in diverse servers based on your project

Recent Comments