dotnet / Scaffolding

Code generators to speed up development.
MIT License
632 stars 226 forks source link

A type with the name "ModelName" does not exist when creating MVC Controller with views #1702

Open ashutoshvyas opened 2 years ago

ashutoshvyas commented 2 years ago

Trying to add a new controller using "Add MVC Controller with views, using Entity Framework" option fails with following error.

There was an error running the selected code generator: 'A type with the name does not exist.

The Code Generation Log is as below:

Finding the generator 'controller'... Running the generator 'controller'... A type with the name SportsWeek.Models.Models.Block does not exist at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

  1. The referenced Model is in a separate assembly.
  2. Controller is generated fine if it is an empty MVC Controller

Microsoft.VisualStudio.Web.CodeGeneration.Design version: 6.0.0 Target framework: .NET 6.0 Operating system: Windows 11 Pro IDE: Visual Studio Community 2022

DarkVoic commented 2 years ago

I have a same error in my code. I trying add a new Controller in my API with project exist

ToddEvansHome commented 2 years ago

I am having the same type of error message. I am trying to add a new controller using "Api controller with actions using entity framework" Error message "There was an error running the selected code generator: 'A type with the name {entity name} does not exist'

Microsoft.VisualStudio.Web.CodeGeneration.Design version: 6.0.0 Target framework: .NET 6.0 Operating system: Windows 10 IDE: Visual Studio Professional 2022

Skeeter-Man commented 2 years ago

Looks to be an issue with "Microsoft.VisualStudio.Web.CodeGeneration.Design version: 6.0.0" and having the models in a separate project such as the Shared project.

A work around is to move your Models into your Server project then add scaffold item. I moved my models similar to below:

The other option i found was to use Net 5, but i would rather use Net 6. In Net 5 it works as expected.

doclamphong commented 2 years ago

I am having the same type of error message. I am trying to add a new controller using "Api controller with actions using entity framework" Error message "There was an error running the selected code generator: 'A type with the name {entity name} does not exist'

Microsoft.VisualStudio.Web.CodeGeneration.Design version: 6.0.0 Target framework: .NET 6.0 Operating system: Windows 10 IDE: Visual Studio Professional 2022

Have you got the answer yet?I have a same error in my code. hix

rzhangdev commented 2 years ago

I have the similar issues when add API controller, the error message from code generation log Finding the generator 'controller'... Running the generator 'controller'... A type with the name xxx does not exist at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

jthreet commented 2 years ago

Having same issue with my project - VS 2022, .net 6, etc. Data layer in separate project. It's like it's not including all namespaces when running generator...

wowraam commented 2 years ago

any solutions or workaround for this problem?

jthreet commented 2 years ago

I can confirm that moving the dbcontext/models into the main project does work. Can't wait for this one to get fixed though.

SwapnilMahant commented 2 years ago

Microsoft.VisualStudio.Web.CodeGeneration.Design version: 6.0.0 Shared folder class not accepting to api controller code . Is there any fix available?

philgraetz commented 2 years ago

Hitting the same problem. Referenced model is in a separate library.

DarkVoic commented 2 years ago

Guys, For me the error occurred in .NetCore 3.1, and what I did to make it work was changing some versions of Nuget Packages.

Try to put the versions of Nuget packages as per the .Net SDK

On Wed, Dec 8, 2021 at 4:40 PM philgraetz @.***> wrote:

Hitting the same problem. Referenced model is in a separate library.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dotnet/Scaffolding/issues/1702#issuecomment-989137339, or unsubscribe https://github.com/notifications/unsubscribe-auth/APWRCFTWMS27JYG5AXZSCF3UP6YBNANCNFSM5IDAF3LQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

philgraetz commented 2 years ago

DarkVoic, not sure what you are suggesting. I am running .NET 6.0 and have NuGet package Microsoft.EntityFramework.Design 6.0.0.

Ucuuba commented 2 years ago

Yep, I’ve got the same problem and versions as @philgraetz We’ve just started migrating all our projects from .NET standard to Core and this is a bit of a pain tbh.

signumq commented 2 years ago

Having this issue as well. The feature is not working if DbContext and Entity classes are in a class library project.

Target framework: .NET 6.0 Operating system: Windows 11 Pro IDE: Visual Studio Community 2022

tristankrass commented 2 years ago

Can confirm. The feature is not working if DbContext and Entity classes are in a class library project.

Target framework: .NET 6.0 Operating system: macOS Monterey, Pop Os 21.04 Running the command from the command line.

limjunk2015 commented 2 years ago

Can also confirm, whilst trying to scaffold API controller in a WASM project - where my models are in the Shared Project.

Microsoft Visual Studio Professional 2022 Version 17.0.0 VisualStudio.17.Release/17.0.0+31903.59 Microsoft .NET Framework Version 4.8.04084

Also reported to VS Developer Community as couldn`t find it reported there... https://developercommunity.visualstudio.com/t/Blazor-WASM-Scaffolded-API-fails-to-buil/1612187

rjbullock commented 2 years ago

Experiencing the same issue. Really annoying because I initially had my models and dbContext in the same project but I separated them out for good "clean architecture". So much for that.

deepchoudhery commented 2 years ago

Issue fixed in the new 6.0.1 package. Reopen the issue if it persists with the new package.

Thanks, Deep

ToddEvansHome commented 2 years ago

Deep, please do not close an issue with the hope that it may be fixed. That is not how professionals handle issues, If the issue has been specifically tested and believed to be fixed then that is a valid reason to close an issue.

Asking to Reopen an issue is unacceptable, especially with the number of people that have voiced their concerns and added comments as well as additional information to the original issue.

On Mon, Dec 20, 2021 at 1:00 PM Deep Choudhery @.***> wrote:

Closed #1702 https://github.com/dotnet/Scaffolding/issues/1702.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/Scaffolding/issues/1702#event-5794697543, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNJOY2P33MS6AO3F2ASOSDUR54MFANCNFSM5IDAF3LQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

-- Thank you, Todd Evans Senior Programmer Media Management, Inc. 314-296-0007

-- https://www.linkedin.com/company/64239  https://twitter.com/MMi_mediaaudit  https://www.facebook.com/MMiMediaAudit  https://plus.google.com/u/0/107322038390188996620/posts  http://www.mediaaudit.com/contact/watchdog-eblast/This e-mail and any files transmitted with it may contain confidential information and is intended solely for use by the individual to whom it is addressed.  If you  received this e-mail in error, please notify the sender, do not disclose the contents to others and delete it from your system.

kyrvlasiuk commented 2 years ago

Not fixed

deepchoudhery commented 2 years ago

@ToddEvansHome I've extensively tested the issue alongside a tester team and we decided that bar is appropriate enough to close an issue but still are looking at feedback. I think asking to simply reopen an issue with a comment is not strenuous for a developer but maybe that is too much to ask yes; I'll reconsider my flow moving forward.

@kyrvlasiuk What is the error message you are currently receiving? I tested again with models and db contexts in referenced projects and no issues.

ToddEvansHome commented 2 years ago

I concur that the scaffolding is not working, although a new error now displays Error "There was an error running the selected code generator: "no database provider has been configured for this DbContext....." My current services.AddDbContext works while running the application, but appears that the scaffolding does recognize it.

deepchoudhery commented 2 years ago

@ToddEvansHome would you mind sharing the services.AddDbContext statement here. I'm curious as to:

Scaffolding is very rigid and does not have a lot of support for custom DbContext classes + custom configurations. We are trying to improve and add support where we can so would definitely appreciate the feedback.

Thanks, Deep

ToddEvansHome commented 2 years ago

Thank you for reopening this. I am using Sql Server Below is our configurations:

AddDbContext statement services.AddDbContext((serviceProvider, options) => {

options.UseSqlServer(Configuration.GetConnectionString("CircleAudit")); options.EnableSensitiveDataLogging(true); }, ServiceLifetime.Scoped);

appsettings..json file* "ConnectionStrings": { "CircleAudit": "Data Source=localhost;Initial Catalog=CircleAudit; Persist Security Info=False;User ID=XYZ;Password=*****;application name=EntityFramework;" },

CircleAuditContext public partial class CircleAuditContext : DbContext { public CircleAuditContext() { }

    public CircleAuditContext(DbContextOptions<CircleAuditContext>

options) : base(options) { } . . . }

Note: The application currently works as expected with the above configurations. Meaning we perform CRUD Api calls without any issues. The problem arises when we try to perform scaffolding with EF in another project.

Repo: 2 projects

In API Project,

Please let me know if you need further information. Thank you for your help!

On Wed, Jan 5, 2022 at 6:05 AM Deep Choudhery @.***> wrote:

@ToddEvansHome https://github.com/ToddEvansHome would you mind sharing the services.AddDbContext statement here. I'm curious as to:

  • What type of database you are using? We only support SqlServer officially but Sqlite also should work. Anything else would definitely cause issues.
  • If you have any additional options configured.
  • The ConnectionString it hooks up to is valid.
  • Any other custom details.

Scaffolding is very rigid and does not have a lot of support for custom DbContext classes + custom configurations. We are trying to improve and add support where we can so would definitely appreciate the feedback.

Thanks, Deep

— Reply to this email directly, view it on GitHub https://github.com/dotnet/Scaffolding/issues/1702#issuecomment-1005628116, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNJOY6D43TA7AHDOU46PD3UUQXXXANCNFSM5IDAF3LQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

-- Thank you, Todd Evans Senior Programmer Media Management, Inc. 314-296-0007

-- https://www.linkedin.com/company/64239  https://twitter.com/MMi_mediaaudit  https://www.facebook.com/MMiMediaAudit  https://plus.google.com/u/0/107322038390188996620/posts  http://www.mediaaudit.com/contact/watchdog-eblast/This e-mail and any files transmitted with it may contain confidential information and is intended solely for use by the individual to whom it is addressed.  If you  received this e-mail in error, please notify the sender, do not disclose the contents to others and delete it from your system.

MaVaWam commented 2 years ago

Issue is still not fixed. The thing that fixed it for me is deleting Visual Studio completely, forgetting about .Net and using Javascript instead. Programming in .Net is a burden, ALL the time. Good riddance!

"Put it all in one project and it works!" Are you kidding me, this is pathetic.

shanemwebster commented 1 year ago

This is still an issue with Dotnet 7.0.102, you still can't have your models in a separate project, which is insane when you are trying to use the entire dotnet eco system. I'm seriously tempted to just go to python and JS at this point, this is insanity.

shanemwebster commented 1 year ago

This is still an issue with Dotnet 7.0.102, you still can't have your models in a separate project, which is insane when you are trying to use the entire dotnet eco system. I'm seriously tempted to just go to python and JS at this point, this is insanity.

rjbullock commented 1 year ago

That is pretty annoying. I haven't tried it since my initial problem but if it still persists that is indeed a hard fail.

MaVaWam commented 1 year ago

In all fairness, it is working for me now in a way. I cannot select the models when they are referenced in a Nuget package when adding controllers with the scaffolder.

However, the following bug was fixed during one of the VS updates. Adding my models project as a project reference to both my API project and Data project (with DBContext), my models show up in the scaffolder and I can create my controllers. So now it's still annoying, but atleast I only have to remove the nuget reference, and reference my project directly.

ahsonsheikh commented 1 year ago

Got the same error but I found where that error was coming from. Error was due to the mismatching Class and filename (.cs). Matching their names solved the problem