question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrations :: "Sequence contains no matching element" thrown sometimes when applying migrations

See original GitHub issue

The issue is when I create an index in the migration, in a table that is base class of other entities.

Add-Migration inicial -Force
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.ModelConfiguration.Edm.StorageEntityTypeMappingExtensions.GetPropertyMapping(EntityTypeMapping entityTypeMapping, EdmProperty[] propertyPath)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.<>c__DisplayClass32.<ConfigureIndexes>b__2a(PropertyInfo icp)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureIndexes(DbDatabaseMapping mapping, EntityType entityType)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains no matching element

Steps to reproduce

  1. create a project with this code
  2. enable migrations
  3. add one migration
  4. uncomment the line modelBuilder.Entity().HasIndex(x => x.Description).IsUnique();
  5. add another migration and the error occur.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TesteEFIndice
{
    class Program
    {
        static void Main(string[] args)
        {
        }

    }

    [Table("A")]
    public class A
    {
        [Key]
        public int Id { get; set; }
        public string Description { get; set; }
    }
    [Table("A")]
    public class B : A
    {

    }
    [Table("A")]
    public class C : A
    {

    }

    public class Context : DbContext
    {
        public DbSet<A> A { get; set; }
        public DbSet<B> B { get; set; }
        public DbSet<C> C { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            //modelBuilder.Entity<A>().HasIndex(x => x.Description).IsUnique();
            base.OnModelCreating(modelBuilder);
        }


    }
}

Further technical details

EF 6.2 Database Provider: Sql Server Operating system: Windows 7 32 bits IDE: Visual Studio 2017 15.7.4

github_iconTop GitHub Comments

ajcvickerscommented, Dec 9, 2019

@TechnikEmpire We have not yet done any planning for the next release.

1reaction
TechnikEmpirecommented, Dec 9, 2019

@ajcvickers Thanks but I think you meant @Tiberriver256 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Migrations Error - Sequence contains no ...
I tried to run add-migration and got "Sequence contains no elements". I upgraded to EF 5 and uninstalled the old Migrations package except...
Read more >
Sequence contains no elements, ERROR - Microsoft Q&A
it is an old Entity Framework application that I am trying to reconstruct and get running. Server Error in '/' Application. Sequence contains...
Read more >
SQL Change Automation - error when doing refresh
My error (see below) is almost exactly the same as the OP's. Steve Message = Sequence contains no matching element Data = System.Collections....
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
In this post, we'll look at some pitfalls and ideas EF Core users like yourself may want to consider when developing an application....
Read more >
Database migration: Concepts and principles (Part 1)
A homogeneous database migration is a migration between the source and target databases of the same database technology, for example, migrating ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found