EF -“更新数据库”导致序列包含多个元素。

时间:2021-09-09 02:13:52

I have just finished modifying my models, ran "Update-Database" in the package manager console and BOOM! I received a "Sequence contains more than one element" error. Upon scanning through the console, it did say No pending explicit migrations. which is obviously wrong since I renamed some models. I also found a SingleOrDefault call I don't know where it came from. I commented out my seed method so that's not causing it

我刚刚修改了我的模型,在包管理器控制台运行了“Update-Database”和BOOM!我收到一个“序列包含多个元素”错误。在对控制台进行扫描时,它并没有说没有等待的显式迁移。这显然是错误的,因为我重新命名了一些模型。我还发现了一个单独的或默认的调用,我不知道它来自哪里。我注释掉了我的种子方法,这样就不会产生它

    No pending explicit migrations.
System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass280.<IndexesEqual>b__27d(String c)
   at System.Linq.Enumerable.<>c__DisplayClass7_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second, IEqualityComparer`1 comparer)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.IndexesEqual(ConsolidatedIndex consolidatedIndex1, ConsolidatedIndex consolidatedIndex2, ICollection`1 renamedColumns)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass271.<FindAddedIndexes>b__26d(ConsolidatedIndex i1, ConsolidatedIndex i2)
   at System.Data.Entity.Utilities.DynamicEqualityComparer`1.Equals(T x, T y)
   at System.Linq.Set`1.Find(TElement value, Boolean add)
   at System.Linq.Enumerable.<ExceptIterator>d__72`1.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 element

EDIT After some digging, found out that the error occurs when EF queries the _MigrationHistory table. but shouldn't it expect records since I want to update the schema?

在挖掘之后进行编辑,发现错误发生在EF查询_MigrationHistory表时。但是它不应该期望记录,因为我想更新模式?

Also, does moving the models to another project have an effect?

此外,将模型转移到另一个项目是否会产生效果?

EDIT AGAIN I got tired of finding what is causing the problem so I did a quick workaround

再次编辑,我厌倦了寻找问题的原因,所以我做了一个快速的解决方案

  1. Create a backup script of the data
  2. 创建数据的备份脚本
  3. Delete the current database
  4. 删除当前数据库
  5. ran Add-Migration and Update-Database in the console
  6. 在控制台中运行外接程序和更新数据库。
  7. Renamed the table names in the backup script accordingly
  8. 相应地重命名备份脚本中的表名
  9. Ran the script
  10. 运行脚本

All is working fine now. Just have to figure out why loading is sooooooooooooo annoyingly slow

现在一切都很顺利。只要弄明白为什么加载速度如此之慢就行了

1 个解决方案

#1


1  

This usually happens to me when I have duplicate entries in my database, corresponding to the same entity.

当我的数据库中有与相同实体对应的重复条目时,这通常会发生在我身上。

Deleting the duplicates makes it work. If you have the data script with you please check and confirm if there were duplicate records in the entities you tried to update in your migration script.

删除副本可以使其工作。如果您有数据脚本,请检查并确认是否在您试图在迁移脚本中更新的实体中有重复的记录。

#1


1  

This usually happens to me when I have duplicate entries in my database, corresponding to the same entity.

当我的数据库中有与相同实体对应的重复条目时,这通常会发生在我身上。

Deleting the duplicates makes it work. If you have the data script with you please check and confirm if there were duplicate records in the entities you tried to update in your migration script.

删除副本可以使其工作。如果您有数据脚本,请检查并确认是否在您试图在迁移脚本中更新的实体中有重复的记录。