Those who want to convert the Sql database schema to the EF core using the dotnet core follow these steps:
Run all the commands one after another (the first command is for those who want to create a new project, otherwise you can ignore this and just run other commands from the root folder of your project)
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 database schema in the model folder of your project.
Now you can easily generate the CRUD code by applying the following command:
dotnet aspnet-codegenerator controller -name <MyNewController> -m <ModelName> -dc <MyDbContext> --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Change MyNewController to the desired controller name, ModelName with the model name in the model folder you want to target, and finally MyDbContext with the system-generated context file name available in the Models folder
but before running this command make sure that you have made the necessary changes to the appsettings.json and startup.cs files in your project folder
appsettings.json add after line
"AllowedHosts": "*",
"ConnectionStrings": { "MyDbConnection": "Server=servername\\instancename;Database=My_Database;Trusted_Connection=True;" }
file startup.cs add right before the line
. Services.AddMvc () SetCompatibilityVersion (CompatibilityVersion.Version_2_2);
services.AddDbContext<SwiftRbs_LocalContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection")));
Enjoy!!
Manoj roy
source share