33

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
Benjamin
  • 3,134
  • 6
  • 36
  • 57
  • I have found a way to back track. I will be using some kind of version control from now on. Still have no idea what this error means. – Benjamin Sep 19 '12 at 22:17

9 Answers9

63

This problem occurs to me when a try to define type and size of a column with DataAnnotations.

BAD:

[Column(TypeName="VARCHAR(254)")]
public string ColumnName { get; set; }

OK:

[MaxLength(254)]
[Column(TypeName="VARCHAR")]
public string ColumnName { get; set; }
Lucas
  • 17,277
  • 5
  • 45
  • 40
Fábio Correia
  • 962
  • 7
  • 8
  • 3
    This! This solved it for me. Seems like a bug (?) in the way EF processes data annotations? Thank you for it, anyway! – Adam Szabo Jun 01 '14 at 18:28
  • Yeah I thought that I had gotten the path to my configuration file wrong. 1up! – Eric Bishard Feb 24 '15 at 06:54
  • 8
    This happens with `[Column(TypeName = "decimal(8,2)")]` as well. – M. Mimpen Apr 10 '15 at 12:45
  • This happens with [Column(TypeName = "text")] as well. Just about drove me crazy track this down. – Bob Black Nov 07 '15 at 05:08
  • 1
    #M. Mimpen what did you do with decimal to solve your issue ? – Salik Jan 14 '16 at 07:43
  • 1
    This also happens with [Column(TypeName="boolean")] ... change this to bit – danpop Feb 25 '16 at 11:43
  • As of 12/2/2016- if you use the [EntityFramework Reverse POCO Generator](https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator), you will get classes that have the offending syntax. NOTE: This is not a knock on the extension, it's EXCELLENT! – Mike Devenney Dec 02 '16 at 18:06
  • 1
    received the same error on my fluent configuration file .HasColumnType("varchar(50)").IsOptional() threw an exception but .HasColumnType("varchar").IsOptional().HasMaxLength(50) fixed it – JCherryhomes Dec 17 '16 at 05:27
  • @Salik Look [here](https://stackoverflow.com/questions/9032919/set-decimal16-3-for-a-column-in-code-first-approach-in-ef4-3/9033029#9033029). It works for me :) – Nurul Mar 13 '17 at 07:00
  • in my case HasColumnType("varchar(50)") causes Sequence contains no elements – nikolai.serdiuk May 25 '20 at 16:49
6

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:

  • Changing the name back to the old one
  • Adding the migration (then was able to do it)
  • Updating the database
  • Changing the name of the entity and generating a new migration

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.

Krzysiek
  • 93
  • 1
  • 5
4

In my case it was due to a mistake on my part. In my configuration, I accidentally included a navigation property instead of the property that represented the column, for example:

HasIndex(x => new { x.ID, x.Parent }).IsUnique();

which should have been

HasIndex(x => new { x.ID, x.ParentID }).IsUnique();

It's easy to overlook, especially in more complex scenarios.

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
2

Looks like a bug. Do you mind filing it at http://entityframework.codeplex.com/workitem/list/advanced with details allowing to reproduce the problem?

Pawel
  • 31,342
  • 4
  • 73
  • 104
2

I got this error when using Fluent API in OnModelCreating like this:

modelBuilder.Entity<ApplicationUser>()
    .Property(b => b.Id)
    .HasColumnType("nvarchar(128)");

Changed to this and then everything worked:

modelBuilder.Entity<ApplicationUser>()
    .Property(b => b.Id)
    .HasColumnType("nvarchar")
    .HasMaxLength(128);

Note that using nvarchar(MAX) and similar is not a problem and will not cause this error.

modelBuilder.Entity<ApplicationUser>()
    .Property(b => b.PasswordHash)
    .HasColumnType("nvarchar(MAX)");
Ogglas
  • 62,132
  • 37
  • 328
  • 418
2

I also encountered this because I was trying to run a migration with a class missing the public access modifier.

Once I added this - the error was resolved.

Michael
  • 1,028
  • 18
  • 25
1

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:

  1. Remove (Comment) self referencing foreign keys and scaffold a migration, Update-Database
  2. Rename problem column, Scaffold another migration, Update-Database
  3. Uncomment self referencing foreign keys and scaffold a migration, Update-Database
  4. Remove the code (but leave the migration) generated by steps 1 and 3

Testing the fix:

  1. Update database, target before step #1 migration
  2. Update-Database to most recent migration and make sure it doesn't fail."
Ashkan
  • 3,322
  • 4
  • 36
  • 47
  • I tried this but the migration I scaffolded for step 1 gave the same error http://stackoverflow.com/q/29745049/150342 – Colin Apr 20 '15 at 10:15
1

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.

Izzy
  • 1,764
  • 1
  • 17
  • 31
1

I have encountered the same problem when there is inheritance between two entities and I wanted to create a index on a property on one of them. There is a reported bug on this issue on version 6.3.

When I removed the inheritance or the index, migration was created successfully. As a workaround you may create the index manually.

Pietro
  • 413
  • 2
  • 11