16 September 2018

Database First (Reverse engineering to the existing database) in ASP.NET Core

If you have an existing database (for example in MS SQL server) and you want to "reverse engineer" to that existing database and create its database context in your ASP.NET Core project, one of the simple and faster ways to do it is by installing the following NuGet packages using the Package Manager:

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools 
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design


Then, to create the models or database context of the existing database to a folder named Models, you can use the following command:

Scaffold-DbContext “Server=Your_Server_Name;Database=SampleDb;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

This command will generate a DbContext and POCO of SampleDb from Your_Server_Name database to Models folder.

No comments:

Post a Comment