{"id":1456,"date":"2024-11-20T16:23:19","date_gmt":"2024-11-20T16:23:19","guid":{"rendered":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2024\/11\/20\/dotnet-scaffold-next-generation-project-creation-for-net\/"},"modified":"2024-11-20T16:23:19","modified_gmt":"2024-11-20T16:23:19","slug":"dotnet-scaffold-next-generation-project-creation-for-net","status":"publish","type":"post","link":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2024\/11\/20\/dotnet-scaffold-next-generation-project-creation-for-net\/","title":{"rendered":"dotnet scaffold \u2013 Next Generation Project Creation for .NET"},"content":{"rendered":"<p>Scaffolding in Visual Studio for ASP.NET Core projects has been a long-standing<br \/>\nfeature which was added shortly after ASP.NET Core was<br \/>\nreleased. We have also had support for scaffolding from the command line<br \/>\nfor many years. From that command line experience, we have heard<br \/>\nfeedback from many users that we need an interactive CLI experience for<br \/>\nscaffolding. To that goal, we have been working on a new <strong>interactive<\/strong><br \/>\nCLI tool, dotnet scaffold. This CLI tool has now been released as a<br \/>\npreview. In this post we will describe how you can acquire and use this<br \/>\nnew command line tool. This tool is open source, you can view the code<br \/>\nin the <a href=\"https:\/\/github.com\/dotnet\/Scaffolding\">scaffolding repo<\/a>. That<br \/>\nrepository contains the code for the dotnet scaffold tool as well as<br \/>\nother scaffolding related code.<\/p>\n<h2>Installing dotnet scaffold<\/h2>\n<p>To install this tool we will use the dotnet tool command. Execute the<br \/>\ncommand below to get the latest released version installed.<\/p>\n<p>dotnet tool install &#8211;global Microsoft.dotnet-scaffold<\/p>\n<p>To install a specific version, visit the <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.dotnet-scaffold\">package on<br \/>\nnuget.org<\/a>.<br \/>\nTo install a specific version, visit the <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.dotnet-scaffold\">package on<br \/>\nnuget.org<\/a>.<br \/>\nFor more info on how to manage dotnet tools see the docs at <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/tools\/global-tools\">.NET<br \/>\ntools \u2013 .NET CLI | Microsoft<br \/>\nLearn<\/a>.<br \/>\nIn the command above we are installing the tool globally, but you can<br \/>\nalso install tools local to a folder. The .NET tools docs has more info<br \/>\non both of those approaches.<br \/>\ntools \u2013 .NET CLI | Microsoft<br \/>\nLearn](https:\/\/learn.microsoft.com\/dotnet\/core\/tools\/global-tools).<br \/>\nIn the command above we are installing the tool globally, but you can<br \/>\nalso install tools local to a folder. The .NET tools docs has more info<br \/>\non both of those approaches.<\/p>\n<h2>Using dotnet scaffold<\/h2>\n<p>By default, dotnet scaffold is an interactive tool, meaning that you<br \/>\ninvoke it, and it will prompt you for info as needed. For automation,<br \/>\nyou can pass in all the parameter values, but we will discuss that in a<br \/>\nfuture post. Before running dotnet scaffold, make sure to change<br \/>\ndirectories (cd) to a folder where a .NET Core project exists. dotnet<br \/>\nscaffold has support for the following ASP.NET Core project types.<\/p>\n<p>Web app<br \/>\nWeb API<br \/>\n.NET Aspire<br \/>\nBlazor<\/p>\n<p>In this post we will focus on the Web app option to show you around<br \/>\ndotnet scaffold, but all the scaffolders follow the same patterns and<br \/>\nprompts.<\/p>\n<p>I created a new ASP.NET Core 9 web app using dotnet new with the command<br \/>\ndotnet new webapp -o MyWebApp. Then I used <strong>cd<\/strong> to change into that<br \/>\ndirectory. When you run dotnet scaffold, you\u2019ll be prompted to select<br \/>\nthe scaffolding category. See the screenshot below.<\/p>\n\n<p>In the image above dotnet scaffold shows a list of the scaffolding<br \/>\ncategories that are currently supported. To navigate this menu, you will<br \/>\nuse the up and down arrow keys on your keyboard to select the desired<br \/>\ncategory. In the future as we add more scaffolders more categories may<br \/>\nappear. Here you\u2019ll select the category of what you are wanting to<br \/>\ngenerate into your project. For example, let\u2019s explore the Razor Pages<br \/>\noption. To select a category, navigate to it and type Return. That will<br \/>\ntake you into the selected option. If you select an incorrect option,<br \/>\nyou can always use Back under Navigation to go back to the previous<br \/>\nselection. After entering the Razor Pages option, you will see the<br \/>\nfollowing options.<\/p>\n\n<p>Now dotnet scaffold is prompting for the specific scaffolder which<br \/>\nshould be invoked. In this case we see two options, the first option is<br \/>\nto create a new empty Razor Page. The second option will generate Razor<br \/>\nPages based on a model class that is a part of the project. Values will<br \/>\nbe persisted using Entity Framework into the selected database provider.<br \/>\nLet\u2019s first run through the Empty scaffolder and then the CRUD one. The<br \/>\nempty scaffolder will generate a new Razor page with an associated code<br \/>\nfile. This is equivalent to running dotnet new page. The generated files<br \/>\nwill not have any customizations.<\/p>\n<p>When you have the option <strong>Razor Page \u2013 Empty<\/strong>, selected hit the Return<br \/>\nkey to go into that option. After that you\u2019ll be prompted to select the<br \/>\ntarget project.<\/p>\n\n<p>In this case we only have one, so we will select MyWebApp and hit the<br \/>\nReturn key. Then it will prompt for the name of the Razor Page to<br \/>\ncreate. Give it the name About and hit the Return key. You\u2019ll see the<br \/>\ncommand working and then you should see the result below.<\/p>\n\n<p>After this has completed you should see the files About.cshtml and<br \/>\nAbout.cshtml.cs in the current working directory. When running this<br \/>\nscaffold, it will use the current directory as the output location. Now<br \/>\nlet\u2019s move on to see how the Razor Page EF option behaves.<\/p>\n<p>Before invoking the EF scaffolders you need a model class for it to<br \/>\nscaffold content for. I\u2019ve created a new web app with the same command<br \/>\nas before, dotnet new webapp -o MyWebApp and I\u2019ve added the following<br \/>\nclass in the root of the project.<\/p>\n<p>namespace MyWebApp;<\/p>\n<p>public class Contact<br \/>\n{<br \/>\n    public int Id { get; set; }<\/p>\n<p>    public string? Firstname { get; set; }<\/p>\n<p>    public string? Lastname { get; set; }<\/p>\n<p>    public string? Email { get; set; }<br \/>\n}<\/p>\n<p>This is a very basic model class that can be used with Entity Framework.<br \/>\nNow we are ready to invoke dotnet scaffold. After starting it, select<br \/>\nthe Razor Pages category and then the <strong>Razor Pages with Entity<br \/>\nFramework (CRUD)<\/strong> option. It will prompt you to select the project,<br \/>\nsince there is only one project in this case you can hit Return because<br \/>\nit\u2019s already selected. Next dotnet scaffold will prompt you to select<br \/>\nthe model class as shown below.<\/p>\n\n<p>Select Contact and hit the Return key to proceed. The next screen will<br \/>\nprompt you for the name of the database context. For this example, give<br \/>\nit the name ContactDbContext and hit the Return key. It\u2019s recommended<br \/>\nfor this value to end with DbContext based on convention, but that is<br \/>\nnot required. See the following image.<\/p>\n\n<p>After you hit the Return key, you will be prompted to select the<br \/>\ndatabase provider.<\/p>\n\n<p>The following list summarizes the options on that screen.<\/p>\n<p>npgsql-efcore = PostgreSQL<br \/>\nsqlserver-efcore = SQL Server<br \/>\nsqlite-efcore = SQLite<br \/>\ncosmos-efcore = Cosmos DB<\/p>\n<p>To keep things simple, we will select the sqlite-efcore option. SQLite<br \/>\nis a file based database and doesn\u2019t have any external dependencies.<br \/>\nSelect that option and hit Return. You\u2019ll be prompted to select what<br \/>\noperations should be scaffolded. See the next image.<\/p>\n\n<p>You can select an individual item to be created, or you can select the<br \/>\nCRUD option to scaffold the full set of pages. Select the CRUD option.<br \/>\nNext, you\u2019ll be prompted if you want to include prerelease packages.<br \/>\nSince we are working with .NET 9 preview, select Yes for that option and<br \/>\nhit Return. After that the scaffolding operation will start. You\u2019ll see<br \/>\na spinner showing that it is running, and it will emit messages for the<br \/>\noperations that are taking place. When it is complete you should see a<br \/>\nresult similar to the following screenshot.<\/p>\n\n<p>After performing this operation, the following changes will have been<br \/>\napplied to the project.<\/p>\n<p>The project file has package references added for Entity Framework<br \/>\nProgram.cs has been updated to initialize the database connection<br \/>\nappsettings.json has been updated with connection information<br \/>\nContactDbContext.cs has been created and added to the project root<br \/>\ndirectory<br \/>\nRazor Pages for CRUD operations has been added to the Pages folder<\/p>\n<p>The content has been generated but the database has not yet been<br \/>\ninitialized. To prep the database, we need to create a migration and<br \/>\nthen update the database. Do that with the following commands.<\/p>\n<p>dotnet ef migrations add initialMigration<\/p>\n<p>This will add a new migration named initialMigration. You can give<br \/>\nit whatever name you prefer here.<\/p>\n<p>dotnet ef database update<\/p>\n<p>This will apply the migrations to the database<\/p>\n<p>After running those commands, you should be ready to run the app with<br \/>\nthe <strong>dotnet run<\/strong> command. After the app starts the URL will be shown<br \/>\nin the terminal, open that URL in your browser with \/ContactPages at the<br \/>\nend of the URL. You should see something like the following.<\/p>\n\n<p>Using this page you can create new contacts and manage existing ones as<br \/>\nwell. After you add some contacts they will appear on this page as shown<br \/>\nbelow.<\/p>\n\n<p>Now you have an ASP.NET Core Razor Pages web app which can manage a<br \/>\ncontact list. After scaffolding you will likely want to customize the<br \/>\ngenerated content to suit your needs. You can use dotnet scaffold to<br \/>\nscaffold other content as you develop your app.<\/p>\n<h2>Recap<\/h2>\n<p>In this post we have used dotnet scaffold to add Razor Pages to a new<br \/>\nASP.NET Core .NET 9 web app. Keep an eye on this blog for more<br \/>\ninformation on changes to dotnet scaffold. We are hoping that you will<br \/>\ntry out this new command line tool and provide us some feedback so that<br \/>\nwe can improve this going forward. In particular, we are very interested in<br \/>\nfeedback on the interactivity of this command line tool.<br \/>\nThe following section has info on how you can provide feedback.<\/p>\n<h2>Feedback<\/h2>\n<p>You can share feedback with us by filing an issue in the <a href=\"https:\/\/github.com\/dotnet\/Scaffolding\">Scaffolding<br \/>\nrepo<\/a>. You can also send feedback<br \/>\nvia the <a href=\"https:\/\/developercommunity.visualstudio.com\/home\">Developer<br \/>\nCommunity<\/a>: report any<br \/>\nbugs or issues via <a href=\"https:\/\/docs.microsoft.com\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio\">report a<br \/>\nproblem<\/a><br \/>\nand share your <a href=\"https:\/\/developercommunity.visualstudio.com\/report?space=8&amp;entry=suggestion\">suggestions for new<br \/>\nfeatures<\/a><br \/>\nor improvements to existing ones. Let us know what you think in the comment below.<\/p>\n<p>The post <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/introducing-dotnet-scaffold\/\">dotnet scaffold \u2013 Next Generation Project Creation for .NET<\/a> appeared first on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\">.NET Blog<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Scaffolding in Visual Studio for ASP.NET Core projects has been a long-standing feature which was added shortly after ASP.NET Core [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1456","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/1456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/comments?post=1456"}],"version-history":[{"count":0,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/1456\/revisions"}],"wp:attachment":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/media?parent=1456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/categories?post=1456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/tags?post=1456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}