command: add-migration blahblah -verbose
error: Sequence contains no elements
I did a few things before getting this error. I made a change to my code-first model but did not runadd-migration
yet. Then I added an EDMX model to play around with an idea visually. I realized the EDMX model was messing with my code so I removed it. I tried to run add-migration
and got "Sequence contains no elements". I upgraded to EF 5 and uninstalled the old Migrations package except for my configurations. Then I tried add-migration
again and I am still getting "Sequence contains no elements". Below is the rest of the error.
System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GetQualifiedTableName(XDocument model, String entitySetName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindRenamedIndependentAssociationColumns>b__ba(<>f__AnonymousType16`2 <>h__TransparentIdentifieraa)
at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains no elements
This problem occurs to me when a try to define type and size of a column with
DataAnnotations
.BAD:
OK:
I ran into the same problem, I found this issue: Migrations: "Sequence contains no elements" in model differ after renaming PK property when self-referencing relationship with no FK property (independent association) which is the situation that I'm stuck with. it's a bug but there's a workaround suggested by a user which I think is a good solution:
"My table was self referencing, dropping the foreign key column and then adding a migration fixed the issue and allowed a rename of the property
Steps to fix, if it helps:
Testing the fix:
This has happened to me when I was doing it for a small web project.
The web's .csproj failed to load one time and visual studio decided to change the default project to a random one that did load.
Because the app.config in the new default project did not contain any connectionStrings... EF could not find anywhere to update.
Changing my default project back to my web project, by right clicking the project and selecting Set As Startup Project, solved this for me.
But I guess the issue here was that EF was looking for the first connection string in the default config, and the number of strings available was 0.
What was causing the issue for me was changing the name together with changing the relations associated with the entity.
So my resolution in my case was:
There can be potentially many other causes of this issue, though - e.g. removing the migration after it was applied. Unfortunately, in none of them this error is meaningful.
I got this error when using Fluent API in
OnModelCreating
like this:Changed to this and then everything worked:
Note that using
nvarchar(MAX)
and similar is not a problem and will not cause this error.Looks like a bug. Do you mind filing it at http://entityframework.codeplex.com/workitem/list/advanced with details allowing to reproduce the problem?