how to use Microsoft.VisualStudio.Web.CodeGeneration?
Asked Answered
M

2

15

I find that The package:Microsoft.VisualStudio.CodeGeneration has over 20,000,000 downloads in the nuget.but I can not find any doc.

how to use it?

any articles about it?

Mensal answered 22/10, 2019 at 1:52 Comment(0)
L
20

What does this package do

Basically, the package offers a single command to generate code:

dotnet aspnet-codegenerator {name}

You can find the source code here.

How to use the package

We don't use the Microsoft.VisualStudio.Web.CodeGeneration directly unless we're creating a new command to generate code.

Because it is a command library for generic purpose, concrete commands are defined in other packages. For example, the dotnet aspnet-codegenerator controller command is defined in Microsoft.VisualStudio.Web.CodeGenerators.Mvc. And the command dotnet aspnet-codegenerator identity is also defined in the CG.MVC package.

Usually , since this package is a generic purpose library, you won't reference this package directly. Instead, You'll add the package Microsoft.VisualStudio.Web.CodeGeneration.Design. Be aware the Microsoft.VisualStudio.Web.CodeGeneration.Design package has a dependency on Microsoft.VisualStudio.Web.CodeGenerators.Mvc, and the Microsoft.VisualStudio.Web.CodeGenerators.Mvc depends on Microsoft.VisualStudio.Web.CodeGeneration:

Microsoft.VisualStudio.Web.CodeGeneration.Design
   | 
   |(depends on)
   |-----------> Microsoft.VisualStudio.Web.CodeGenerators.Mvc
                | 
                |(depends on)
                |-----------> Microsoft.VisualStudio.Web.CodeGeneration

Be aware the Microsoft.VisualStudio.Web.CodeGeneration.Design is automatically added into your dependencies when you use Visual Studio to scaffold a controller/identity.

If you're using a VSCode/CLI, you need to manually add such a package reference. See https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-3.0&tabs=visual-studio-code#add-nuget-packages

Loach answered 23/10, 2019 at 2:42 Comment(2)
How does one remove it from a project?Loki
Can somebody explain why isn't this added only as desing-time-only dependency?Laggard
B
0

What @itminus says. I think the reason it has so many downloads is from the clear error message when you try to use dotnet aspnet-codegenerator without Microsoft.VisualStudio.Web.CodeGeneration.Design NuGet.

As of 2021-08-27 it has 90,672,569 total downloads.

https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/

Example error:

Building project ...

Scaffolding failed.

Add Microsoft.VisualStudio.Web.CodeGeneration.Design package to the project as a NuGet package reference. To see more information, enable tracing by setting environment variable 'codegen_trace' = 1.

Backsaw answered 27/8, 2021 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.