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

Created on 6 Jul 2018  路  9Comments  路  Source: dotnet/ef6

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.

```c#
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

Most helpful comment

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

All 9 comments

We have the exact issue. I see it has been under investigation since Aug 16. Are there any updates on this?

@mb34124 The solution is to not double-configure with both the fluent API and annotations.

I ran into this issue as well and that was the problem. In the example here, this comes up because in most drivers, [key] also denotes an indexed column.

While this is a valid bug, the solution is to simply stop telling EF to configure things twice.

Is this targeted for the 6.5.0 release?

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

@ajcvickers Thanks but I think you meant @Tiberriver256 :)

@TechnikEmpire Hi! I tried deleting of annotations and it didn't help. And before the problem, I had both annotations and fluent API, then I just changed an entity on which I used .HasIndex().IsRequired() and got this error.

@myNckname sorry fam it's been a year since I ran into this issue and virtually all memory of it is lost. Cant help.

Hi @ajcvickers, i'm running into this still on EF 6.4.0. Any updates? Thanks.

Same issue, and this is causing us quite a headache.

Was this page helpful?
0 / 5 - 0 ratings