0

ASP.NET Core Web App targeting .Net 7. Started with files as a part of the create new project template. Other than the pages (cshtml & cs) files that came with the template, have only created few dozen Models and a bunch of meta info in ApplicationDbContext that further defines the database fields. For example:

b.Property<string?>("Mailing_City")
                    .HasMaxLength(50)
                    .HasColumnType("VARCHAR")
                    .HasColumnName("Mailing_City")
                    .HasComment("Indicates the client's current city to send mail.");

While creating the Models, have added/removed migrations as well as updated the database without issue many times. Now that all the models are finished, went to update NuGet packages to the latest stable version. Any package that depends on Microsoft.EntityFrameworkCore 7.0.3 or higher seems to produce the 'Sequence contains no elements' error. The specific error from Package Manager Console:

System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.Format(IReadOnlyModificationCommand command, StringBuilder builder)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.FormatCycle(IReadOnlyList`1 data)
   at Microsoft.EntityFrameworkCore.Utilities.Multigraph`2.ThrowCycle(List`1 cycle, Func`2 formatCycle, Func`2 formatException)
   at Microsoft.EntityFrameworkCore.Utilities.Multigraph`2.TopologicalSortCore(Boolean withBatching, Func`4 tryBreakEdge, Func`2 formatCycle, Func`2 formatException)
   at Microsoft.EntityFrameworkCore.Utilities.Multigraph`2.BatchingTopologicalSort(Func`4 tryBreakEdge, Func`2 formatCycle, Func`2 formatException)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.TopologicalSort(IEnumerable`1 commands)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(Boolean forSource, DiffContext diffContext)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(IRelationalModel source, IRelationalModel target, DiffContext diffContext)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Sequence contains no elements

Reinstalling package version of 7.0.2 allows a migration to be successful.

Searching for possible fixes point to using .FirstOrDefault instead of .First for the result of a query as well as the various query criteria that would fail when there are no results. Have searched the entire solution for first(, single(, etc as suggested in Sequence contains no elements? and other pages. The only places where these are listed is in bootstrap or jquery files that came as a part of the template.

Have checked make sure that data annotations are like

[Column("Msg", TypeName = "varchar")]
[MaxLength(1024)]

instead of [Column("Msg", TypeName = "varchar(1024)")]. This has been checked in both the Model and ApplicationDbContext files.

Online searches have not come up with anything specific to 7.0.3.

I'd like to fix this error, bringing the packages up to date with the most current, before I start building the actual pages that would have the queries where this could be an error.

Any pointers on additional things to look for? Any additional information needed?

Michael
  • 51
  • 3
  • @Michel, Hello, could you please mention the specific line of code that triggers the ‘Sequence contains no elements’ error? – Laaziv May 07 '23 at 00:05
  • @Laaziv that is what I'm trying to determine. Executing a 'Add-Migration blahblah' command in Package Manager Console produces the error noted above. No specific line is given in the error. No clue how to find it. – Michael May 07 '23 at 00:18
  • @Michel have you tried updating all ASP.NET packages and running the application without performing a migration to see if there are no errors? – Laaziv May 07 '23 at 00:28
  • @Laaziv - Just did that; no errors, and can click through the links (privacy, register, & login) without issue. – Michael May 07 '23 at 01:05

0 Answers0