4

We've recently done a large refactor on our codebase (adding some new entities, updating namespaces, changing navigation properties etc.)

Our codebase prior to the refactor had a number of entity framework migrations (which were working fine).

However, upon trying to run 'update-database' or 'add-migration' on the refactored codebase, we received a Sequence contains more than one matching element exception.

The only way we could resolve this issue was by completely removing the existing (previously working) migrations, and running 'add-migration'. This obviously generated a new database schema from scratch, but the migration worked as normal.

Is there a way we can move forward without removing our existing migrations?

Find the stack trace from running 'update-database' below:

System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass24f.<FindAlteredColumns>b__246(<>f__AnonymousType2b`2 <>h__TransparentIdentifier241)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.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(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion)
   at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Sequence contains more than one matching element

Edit: I've now tried disabling all seed data statements and the issue still exists.. Also a related issue has been found here: http://entityframework.codeplex.com/workitem/569 (although no solution is given).

  • `SingleOrDefault` must have only one element in database. IF 2 are returned, an exception is thrown. – Deblaton Jean-Philippe Apr 28 '17 at 08:39
  • Yes - but none of our migrations actually use 'SingleOrDefault'. I think its being called by entity framework itself? – Blake Williams Apr 28 '17 at 08:45
  • Have you define size of a property using `ColumnAttribute` (e.g. `[Column(TypeName="VARCHAR(50)")]`? How you execute `Add-Migration` command (include migration name too)? – Tetsuya Yamamoto Apr 28 '17 at 08:50
  • Nope we've just left it as the Entity Framework default... The same exception occurs when we run 'update-database' too.. so I don't think it has to do with how we actually ran the command.. – Blake Williams Apr 28 '17 at 08:57
  • Is your problem similar to this: http://entityframework.codeplex.com/workitem/569? Does your table has independent association(s)? Usually this is related to model generation or `DbMigration`, show your model & migration code which extends `DbMigration` inside `Migrations` directory. – Tetsuya Yamamoto Apr 28 '17 at 09:04
  • Yes that's exactly the issue I think.. I saw that a while back but it didn't really offer a solution? – Blake Williams Apr 28 '17 at 09:05
  • We've got around 20 migrations - which ran ok before.. I can't really determine which table is causing the issue as we've made changes to a whole bunch of parts of the app.. – Blake Williams Apr 28 '17 at 09:06
  • Your issue is related to seed data. Check your configuration's seed method. The error should tell you the exact line number where it fails. This should give you an idea which table/entity has duplicate data. – Amanvir Mundra Apr 28 '17 at 12:48
  • Generate a script version of the migrations to troubleshoot (`update-database -Script)`. I would also check the Seed() code as Amanvir suggests. – Steve Greene Apr 28 '17 at 14:39
  • I've disabled all seed data and the issue still exists.. I'll try the script now – Blake Williams Apr 28 '17 at 21:52
  • However I know that the existing migrations did work prior? So there shouldn't be a difference between the scripts right? – Blake Williams Apr 28 '17 at 21:53
  • `update-database -Script` caused the same issue... – Blake Williams Apr 30 '17 at 23:16
  • Run `update-database -verbose` and post the last sql satements it may provide a lead – Modar Na May 03 '17 at 17:29
  • the update-database sql statements are correct - but at the end of the command the exception is thrown (however if I run it again, it says there's no pending migrations - and throws the exception again) – Blake Williams May 04 '17 at 00:51
  • refer to this link https://stackoverflow.com/questions/25233769/sequence-contains-more-than-one-element-error-in-ef-cf-migrations – Stanley Mohlala Sep 19 '17 at 13:00

2 Answers2

0

I had the same issue after much hit and trial I found the issue. The issue was caused by two classes sharing the same name but different namespace; somehow EF ignored the namespace and error occurred after changing the class name the error went away.

irfandar
  • 1,690
  • 1
  • 23
  • 24
  • From the stack trace, this looks like the offending code; https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/Migrations/Infrastructure/EdmModelDiffer.cs#L1873 – Jeremy Lakeman Dec 19 '19 at 13:49
  • I've found the EF Core team fairly responsive to bug reports, particularly when I can trace down the problem code. The .net core codebase will become .NET 5, so it's worth raising bugs there. – Jeremy Lakeman Dec 19 '19 at 14:01
0

This issue may come if EF is getting confused by same name properties for example:

public int Username { get; set; }

public int UserName { get; set; }

may trigger this error.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30