I have tried all the solution like repairing the VS 2013 but no use. when you create a controller by right clicking on the Controller folder and you add the controller, then you right click in the Action of the newly created controller and select Add View, it happens right then, when I try to create a view. Its not a new project, its an existing project.
-
1I had similar error in vs 2015. Building solution after add controller and them add view helped. – Aht Aug 24 '16 at 15:40
-
I have the same issue, i think its related to EF and MySQL driver... – pojomx Nov 14 '16 at 16:50
-
4Restarting VS fixed it for me. – niico Apr 07 '18 at 16:09
25 Answers
For me the error had to do with the fact that I had a project in my solution that was a .NET Core project library and was referenced in the project that housed the DbContext.
Removing - or I guess changing the type - the .NET core library resolved the issue

- 165
- 2
- 10
-
I think this may be the issue I'm encountering ;-( this is the first time I've tried to use the scaffolding with a .NET Core project. It's always worked with the Framework flavored libraries – bkwdesign Feb 14 '18 at 17:48
-
This was exactly the issue for me. I unloaded the Core project, and the re-ran the Code Generator. When it completed, re-loaded the Core project, compiled and voila - it worked. – RC Cola Jun 29 '22 at 19:29
I had this issue on my VS2017 and i solved it by doing this:
Go to C:\Users\username\AppData\Local\Microsoft\VisualStudio\15.0_7fca0c70
and you will see a folder named ComponentModelCache
just renamed it like _ComponentModelCache
or anything else,visual studio will create this folder again.
One thing to notice under VisualStudio folder name 15.0_7fca0c70 can be differenct it depends on your VS version. Now you are good to Go. Hope it helps somebody.

- 5,882
- 3
- 33
- 44

- 155
- 2
- 4
- 12
Visual Studio 2019/2022:
- Clean Project
- Rebuild Project
- Close and reopen Visual Studio 2022
If not works -> update Microsoft.VisualStudio.Web.CodeGeneration.Design NuGet package to latest and try again.

- 371
- 1
- 12
I have the same error in Visual Studio 2017 I fix it by this solution :
1 - if you use any database , must be install all drivers (I mean Nugget packages and etc...) (I use MySql db so i must I first install MySql installer and install MySql,Data,EntityFramWorkCore)
2 - I Update My Visual Studio with Visual Studio Installer (your must be search visual studio installer in start and run it next update (don't need to add any new component or codes only select Update Button ) )
It work for me , I hope this will be help you

- 613
- 8
- 19
Tried all the following answers with no luck:
- Solution Clean
- Deleting old migrations
- Close and reopen Visual Studio
- Delete Visual Studio temp Components folder
This one worked:
- Uninstalled the package (Microsoft.VisualStudio.Web.CodeGeneration.Design) and re-installed it from Package Manager Console.
My environment: Visual Studio 2019 - Razor Pages - .Net Code 2.2 - Windows 10

- 819
- 9
- 11
-
I had to remove GCop as it was referencing an outdated version of Microsoft.VisualStudio.Web.CodeGeneration.Design. – KSwift87 Feb 12 '20 at 00:06
-
@KSwift87 Apparently some component files crashes and needs refreshment – Mohamed Nagieb Mar 15 '20 at 13:28
My issue (which specifically has the 'Object reference not set to an instance of an object.' error) was resolved by changing some of the projects in the Solution from .net standard 2.0 to .Net Framework 4.8.
This was inspired by the answer by Timo Hermans above and having tried the main answer deleting the ComponentModelCache which had helped previously.
In the project I was performing the scaffolding in I traced projects that linked to the Model I was using and changed them to net48. I did not have to change them all, so not entirely sure which projects but I started with the Models project and then followed other key projects changing them back to net48 until the scaffolding worked again.
Hope this might be of use to someone because it was driving me made!

- 49
- 4
-
Same problem here, I had a Standard project referenced and everything worked when I changed it to Framework – rasputino Sep 25 '20 at 23:22
Simple steps that worked for me like charm
- Remove the project from the solution
- Use Add Exisiting project to add the same project back
- Build and Add View now, it works

- 11
- 3
This Error is related to Entity Framework Data Model .. for solution Perform these Steps...
- Delete your Entity Framework Data Model.
- Again Regenerate the Model from Database Design.
- then Rebuild the Solution ... and Enjoy Coding .

- 9
- 3
I have the same error, I just removed only read permission in the folder where is the web project

- 6,982
- 5
- 55
- 87

- 9
- 1
Try installing the Windows Management Framework:
https://www.microsoft.com/en-us/download/details.aspx?id=34595
Restart the PC after installing.

- 7,405
- 1
- 19
- 30
Or maybe like me you didn't notice the 'Data context class' field was blank. Oops. If so, simply select your data context class and try again.
There really should be a better error for this scenario.

- 1,997
- 2
- 18
- 12
Removed the error by starting a whole new project because I had initially referenced a Class Library that housed the data access and business logic code for my project.

- 1
- 1
I have the same error in vs2017 on windows10, pls try to run vs as admin, hope it'll work for you too.

- 21
-
don't know why this is being downvoted - this worked for me in VS2019 preview - cheers Frank – Tim Morrison Jul 02 '19 at 11:00
Although I had not noticed right away that my ability to add Controllers ceased when I added another project to the solution, I eventually caught on that this was a repeatable cause for my disruption. This is what worked for me:
- In addition to the .Net Web App project where I was adding Controllers, I had added another project to the solution- this causes the problem for me.
- I unloaded the new project and then tried the Add Controller step again. This time it worked.
- I then reloaded the new project and continued to work
It took me a couple of hours to diagnose and solve this. Maybe it helps someone else to not spin wheels this long!

- 35
- 2
Instead of renaming, I left DefaultController then renamed it after it was created.
I had a Standard .Net v2
class library beside my main ASP.Net MVC
project which targeted .Net Framework v4.6.2
and after several controller creation suddenly the mentioned error came up.
So I :
- Removed references to the class library
- Deleted class library entirely
Created another one with
.Net Framework v4.6.2
and the problem solved.

- 1,143
- 2
- 21
- 43
Same error in VS2019, looks like I'd missed a project reference after collapsing three projects into a single solution.
I'd referenced the API and class library in my UI project so I could use the db context (in the API project) and class files for scaffolding, but failed to reference the class library in the API project (it'd previously been added as a NuGet package) and it gave me this error message.
Uninstalled the package and added as a project reference and my issue seems to be fixed.

- 591
- 1
- 6
- 24
I just removed the space** from DATA CONTEXT CLASS. worked for me.
Add View > Data Context Class > REGMVCEntities**(REGISTRATION.Models)

- 1
In the end, my problem was that the project to which I was trying to add a scaffolded item was on a network shared drive / mapped drive. Moving the project to a local disk fixed my problem.
VS 2013 Update 5

- 33
- 7
This issue is just for installation Modification or Packages Update, this is not Bug!
Simple Solution: Update Packages.
Long Way Solution: I did All these steps and Just Last Step was My solution. Im using VS 2022!
Step1: Modify VS (Not Working For me)
1- Run Visual Studio Installer 2- Click Modify 3- On the right-side expand ASP.NET and web development 4- check “.Net Framework project and item templates”
Step2: Rename (Not Working For me) 1- Go to VS Directory at C:\Users\YOURUSER\AppData\Local\Microsoft\VisualStudio 2- Rename Directory Named something like 17.0_d413a229 VS Creates this file again automatically!
Step3: Clean/Rebuild Solution and Project (Not Working For me) After Clean and Rebuild, Reopen VS.
Step4: UPDATE ALL OLD PACKAGES. (Worked Perfectly) Go to your own Solution NUget packages and update all of them.

- 11
- 2
Just change your ConnectionString in Web.Config file From
<add name="xxx" connectionString="Data Source=xxx;initial catalog=xxx;Persist Security Info=True;User ID=xxxx;Password=xxxx;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
To
<add name="xxx" connectionString="metadata=res://*/EFMOdel.csdl|res://*/EFMOdel.ssdl|res://*/EFMOdel.msl;provider=System.Data.SqlClient;provider connection string="data source=xxxx;initial catalog=xxxx;persist security info=True;user id=xxxx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

- 109
- 1
- 3