How to scaffold model from existing database in ASP.NET MVC Core?

 Posted by Sheonarayan on 2/15/2017 | Category: ASP.NET Core Interview questions | Views: 5540 | Points: 40
Answer:

To scaffold models from existing database in ASP.NET MVC Core, go to Tools –> NuGet Package Manager –> Package Manager Console

Now execute following command

Scaffold-DbContext "Server=datbase-PC;Database=TrainingDatabase;Trusted_Connection=True;MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

If you get an error "The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet" , simply Close the Visual Studio and open again.

This creates .cs files related with all database tables into Models folder of your project. This also creates a database context (in this case the name will be TrainingDatabaseContext.cs as the database name is TrainingDatabase).


| Alert Moderator 

Comments or Responses

Login to post response