13

I'm running through one of Microsoft's tutorials on MVC development and I'm getting errors when trying to create various elements; Views, Controllers, etc.

The error I'm getting is the following:

There was an error running the selected code generator: 
'Sequence contains no elements'

It's consistant everytime I try to create a new element. Earlier, I bypassed the error by copying a view and repurposing it. But that's not optimal in the long run!

I'm currently sitting at work doing this tutorial where the issues arise. However, when I started this tutorial at home yesterday (hence, on a different network) I had zero issues. Might that have something to say?

Here's a link to the project on GitHub.

Do you think it's an issues with my network, my project itself, or something completely different?

sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • Sounds like a VS install issue...maybe something went wrong with the installed project templates. – Miklós Tóth Jan 26 '18 at 09:22
  • I'm uninstalling right now to see if a fresh install will resolve the issue! – Patrick Christensen Jan 26 '18 at 09:56
  • I am using ASP.NET MVC 6.0 and VS2022 and receiving the "no sequence" message attemptin to add a view or controller that requires the ApplicationDbContext. None of these solutions worked. – Andy Jan 25 '22 at 21:33

15 Answers15

9

I've upvoted @James's comment because it worked for me, but I wanted to add it as an answer to make it more visible to others:

Open the package manager console:

Tools > Nuget Package Manager > Package Manager Console

Type:

dotnet restore

No restart / reinstall required.

Darren
  • 4,408
  • 4
  • 39
  • 57
7

Visual Studio 2019 and .net core 3.0

Uninstalled: Microsoft.VisualStudio.Web.CodeGeneration.Design Version: 3.1.0-preview3.19558.8

and Installed: Microsoft.VisualStudio.Web.CodeGeneration.Design Version: 3.0.0

Solved the issue for me.

Mohamed Elamin
  • 345
  • 4
  • 7
4

Try clearing the ComponentModelCache, the cache will rebuild next time VS is launched.

  1. Close Visual Studio Delete everything in this folder
  2. C:\Users\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
  3. Restart Visual Studio 14.0 is for visual studio 2015. This will work for other versions also.

Credit to https://stackoverflow.com/a/35815094/5209435

Edit

Are your providerNames in the connectionString section of your web.config file the same?

You may also want to try the suggested solutions from here or here.

Matt Stannett
  • 2,700
  • 1
  • 15
  • 36
  • I tried clearing the cache you mentioned -- No dice! Another user, Miklós, commented that it might be an install issue, so I'm currently uninstalling VS. :) Thanks for the suggestion! – Patrick Christensen Jan 26 '18 at 09:55
  • @PatrickChristensen edited my answer to include some more suggestions. – Matt Stannett Jan 26 '18 at 18:46
  • 2
    This worked for me thanks. I had to clean C:\Users\xxxxx\AppData\Local\Microsoft\VisualStudio\15.0_24fd3914 for VS 2017 ( where xxxxx is your user account name) – mini998 Feb 10 '18 at 12:45
  • I had renamed the folder mentioned by mini998, starts VS2017. It will creates a new folder with similar name. Delete the new folder, then replace with the original one. And the problem is fixed automagically. – s k Apr 03 '18 at 06:01
  • 4
    Running `dotnet restore` in `Tools>Nuget Package Manager>Package Manager Console` fixed this for me. – James Toomey May 17 '18 at 22:14
4

I had the same error in visual studio 2019.

Problem could be fixed by:

  • Close Visual Studio
  • Delete '.vs' directory
  • Open Project
  • Rebuild
JanDotNet
  • 3,746
  • 21
  • 30
  • Thanks! This was the solution for me as the other suggestions didn't work. Opening a visual studio 2017 project in 2019 was causing the error when I tried adding a controller. – hackingchemist Aug 21 '19 at 14:22
1

Error will occur if you had different instances of connection strings in your web.config

Comment out the other not needed or necessary connection strings tags then rebuild your solution. That would be all

1

This worked for me for VS 2017.

Close the opened instance of VS 2017. Open it again. Rebuild the solution and now create View, Controller etc. It will be created now.

kanika
  • 92
  • 1
  • 13
1

Turns out for me that Microsoft.VisualStudio.Web.CodeGeneration.Design was restored using the wrong .NetFramework version. So I launched the package manager console (Tools > NuGet Package Manager > Package Manager Console) and typed Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.0.0

This fixed it for me.

Bmize729
  • 1,126
  • 8
  • 18
1

All the answers above didn't work for me.

However, this is the Error message i got -
"detected package version outside of dependency constraint microsoft.aspnetcore.app 2.1.1 requires microsoft.netcore.razor.design...bla bla bla"

How i overcame it was to install a lower version of razor via nuget package manager:

Steps:

on Visual Studio 2017 Click on Tools > Nuget Package Manager > Package Manager Console then run the command below.

Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 2.1.1

Chidi-Nwaneto
  • 634
  • 7
  • 11
1

To anyone seeing this error when attempting to scaffold an upgraded .net6 project.

  • Scaffolds only work with the new style .net6 Minimal Hosting Model and I still had the .net5 style program and startup files.

This moved me next onto a frustrating EF Core issue 'unable to resolve service' error meaning Scaffold was unable to resolve the EF Contexts despite them being correctly in the new program.cs

Huggster
  • 93
  • 6
0

Uninstalling and reinstalling VS2017 did not work. I can create classes, items and other lesser elements. However, the issues with creating Controllers, View, etc. persists.

0

I had this trying to add an Empty API Controller in an ASP.NET Core Web Application (API template). I found in Project Properties the Target Framework wasn't set. I set it to .NET Core 2.1.

If you then try to add the controller you will get an error because this line of code was added to Startup.cs

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

and CompatibilityVersion doesn't have an enum for Version2_2, so change it to 2_1:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

and now it adds the controller ok.

I was getting this with a totally new project. Visual Studio 2017 15.9.11.

I had already tried the dotnet restore and clearing the ComponentModelCache.

John Mann
  • 31
  • 5
0

you must update or install the version of Microsoft.VisualStudio.Web.CodeGeneration.Design package .

this is the solution, respects .

tahsin ilhan
  • 208
  • 2
  • 12
0

My project had a warning against one of the dependancies (Microsoft.VisualStudio.Web.CodeGeneration.Design). I removed that dependancy and then rebuilt the project.

I then added a new Razor page and the dependancy was readded to the project - with an earlier version (2.1.9) and I can now add Razor pages without issue.

pookemon
  • 131
  • 1
  • 7
0

I have no evidence of the root cause, but the following issue wastes my two days ago to find out the solution.

If there big data table type in your model class, check the following word has existed

[Column(TypeName = "ubigint")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

in your model class. Just take out these two-line, everything is going to be fine.

Kevin
  • 1
  • 1
0

there was an error running the selected code generator:' sequence contains no elements'

Creating Index View

only Select

enter image description here

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 22:21