gpt4 book ai didi

c# - EF - "Update-Database"导致序列包含多个元素

转载 作者:太空狗 更新时间:2023-10-29 23:44:12 25 4
gpt4 key购买 nike

我刚刚修改完我的模型,在包管理器控制台中运行“更新数据库”,然后 BOOM!我收到“序列包含多个元素”错误。扫描控制台后,它确实显示了 No pending explicit migrations. 这显然是错误的,因为我重命名了一些模型。我还发现了一个不知道从哪里来的 SingleOrDefault 调用。我注释掉了我的种子方法,这样就不会导致它

    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

编辑经过一番挖掘,发现错误发生在 EF 查询 _MigrationHistory 表时。但既然我想更新模式,它不应该期待记录吗?

此外,将模型移动到另一个项目有影响吗?

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

  1. 创建数据备份脚本
  2. 删除当前数据库
  3. 在控制台中运行 Add-Migration 和 Update-Database
  4. 相应地重命名备份脚本中的表名
  5. 运行脚本

现在一切正常。只需要弄清楚为什么加载如此慢得令人讨厌

最佳答案

当我的数据库中有与同一实体对应的重复条目时,我通常会遇到这种情况。

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

关于c# - EF - "Update-Database"导致序列包含多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com