事件 ID:1026 System.InvalidOperationException 在 System.Linq.Enumerable.Single

Event ID:1026 System.InvalidOperationException at System.Linq.Enumerable.Single

我对我的 DbContext 进行了以下更改

//public DbSet<Role> Roles { get; set; }  //old
//public DbSet<TypePermissionObject> TypePermissionObjects { get; set; } //old
//public DbSet<User> Users { get; set; } //old

public DbSet<PermissionPolicyRole> Roles { get; set; }
public DbSet<PermissionPolicyTypePermissionObject> TypePermissionObjects { get; set; }
public DbSet<PermissionPolicyUser> Users { get; set; }

现在下面的代码行返回错误。当它应该返回 false 时。

var compatible = db.Database.CompatibleWithModel(false); 

调用栈为

Application: MyApp2.Win.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
   at System.Linq.Enumerable.Single[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer+<GetStoreAssociationTypePairs>d__15.MoveNext()
   at System.Linq.Enumerable+<SelectManyIterator>d__23`3[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()
   at System.Linq.Enumerable+<DistinctIterator>d__64`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()
   at System.Collections.Generic.List`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at System.Linq.Enumerable.ToList[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.FindAssociationTypePairs(System.Collections.Generic.ICollection`1<System.Tuple`2<System.Data.Entity.Core.Metadata.Edm.EntityType,System.Data.Entity.Core.Metadata.Edm.EntityType>>)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata, ModelMetadata, System.Lazy`1<System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator>, System.Data.Entity.Migrations.Sql.MigrationSqlGenerator, System.String, System.String)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(System.Xml.Linq.XDocument, System.Xml.Linq.XDocument, System.Lazy`1<System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator>, System.Data.Entity.Migrations.Sql.MigrationSqlGenerator, System.String, System.String)
   at System.Data.Entity.Internal.InternalContext.ModelMatches(System.Data.Entity.Migrations.Infrastructure.VersionedModel)
   at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean, System.Data.Entity.Internal.DatabaseExistenceState)
   at System.Data.Entity.CreateDatabaseIfNotExists`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].InitializeDatabase(System.__Canon)
   at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(System.Action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.RetryAction`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].PerformAction(System.__Canon)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(System.Action`1<System.Data.Entity.Internal.InternalContext>)
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(System.Type)
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Count[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.IQueryable`1<System.__Canon>)
   at MyApp2.Win.Program.Main()

我仔细检查了连接字符串

项目参考EntityFramework6.4.4

事件查看器显示错误源是 .NET 运行时 使用 Process Monitor 的输出(来自 sysinterenals.com )交叉检查错误时间,我看到 EntityFramework.pdb 的 NAME NOT FOUND 错误 4 毫秒之前,BiDInterface

出现 NAME NOT FOUND 错误

this question 的答案表明我的数据库中可能有重复条目。我不太清楚是什么意思。

我尝试更改为以下但没有帮助/

  public DbSet<PermissionPolicyRole> Roles2 { get; set; }
  public DbSet<PermissionPolicyTypePermissionObject> TypePermissionObjects2 { get; set; }
  public DbSet<PermissionPolicyUser> Users2 { get; set; }

[更新]

我决定删除迁移并生成一个新迁移。 当我尝试这样做时,我在 PM 控制台中收到以下错误

PM> add-migration security
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<GetStoreAssociationTypePairs>d__15.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.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.FindAssociationTypePairs(ICollection`1 entityTypePairs)
   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.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Infrastructure.Design.Executor.ScaffoldInternal(String name, DbConnectionInfo connectionInfo, String migrationsConfigurationName, Boolean ignoreChanges)
   at System.Data.Entity.Infrastructure.Design.Executor.Scaffold.<>c__DisplayClass0_0.<.ctor>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
Sequence contains no matching element
PM> 

我在 DbContext 中有以下内容

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Configurations.Add(new RoleConfiguration());
    modelBuilder.Configurations.Add(new UserConfiguration());
}

public class RoleConfiguration : EntityTypeConfiguration<DevExpress.Persistent.BaseImpl.EF.PermissionPolicy.PermissionPolicyRole>
{
    public RoleConfiguration()
    {
        HasMany(x => x.Users).WithMany(y => y.Roles).Map(x => x.ToTable("PermissionPolicyUserPermissionPolicyRoles"));
    }
}

public class UserConfiguration : EntityTypeConfiguration<DevExpress.Persistent.BaseImpl.EF.PermissionPolicy.PermissionPolicyUser>
{
    public UserConfiguration()
    {
        HasMany(x => x.Roles).WithMany(y => y.Users).Map(x => x.ToTable("PermissionPolicyUserPermissionPolicyRoles"));
    }
}

[更新]

研究了一个新的xaf项目,发现不需要配置。

现在我尝试 运行 在没有我得到的配置文件的情况下进行迁移

PM> 添加迁移安全性

 ystem.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<GetStoreAssociationTypePairs>d__15.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.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.FindAssociationTypePairs(ICollection`1 entityTypePairs)
   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.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Infrastructure.Design.Executor.ScaffoldInternal(String name, DbConnectionInfo connectionInfo, String migrationsConfigurationName, Boolean ignoreChanges)
   at System.Data.Entity.Infrastructure.Design.Executor.Scaffold.<>c__DisplayClass0_0.<.ctor>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)
Sequence contains no matching element
PM> 

我认为安全 table 升级的顺序错误。

我需要回滚并更加小心地进行。