Those who want to convert Sql database schema to EF core using dotnet core follow the steps:
Run all the commands one after one (first command for those who want to create a new project else you can ignore that and just run other given commands from your project root folder)
dotnet new mvc -o <ProjectName>
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet tool install --global dotnet-aspnet-codegenerator
Finally...
dotnet ef dbcontext scaffold "Server=servername\instancename;Database=My_Database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
This will create the necessary models and context of your db schema inside the models folder of your project.
Now easily you can generate CRUD code by applying the following command :
dotnet aspnet-codegenerator controller -name <MyNewController> -m <ModelName> -dc <MyDbContext> --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Change the MyNewController with your desired controller name , ModelName with the model name inside the models folder that you want to target and finally MyDbContext with the system generated context file name available inside the Models folder
but before run that command make sure you have made the necessary changes at your appsettings.json and startup.cs file inside your project folder
appsettings.json add after the line
"AllowedHosts": "*",
"ConnectionStrings": {
"MyDbConnection": "Server=servername\\instancename;Database=My_Database;Trusted_Connection=True;"
}
startup.cs file add just before the line
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<SwiftRbs_LocalContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection")));
Enjoy!!