序列包含多个匹配元素-EF数据库初始化

时间:2018-11-01 22:14:35

标签: c# entity-framework dbcontext

我正在使用一个在初始化时生成数据库的系统:

public static void initialiseEntityFramework()
{
    using (DbContextentityframework context = new DbContextentityframework())
    {
        context.Database.Initialize(true);
    }
}

但是在初始化时返回错误:

  

System.InvalidOperationException:'序列包含多个   匹配元素”

堆栈跟踪:

at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.ForeignKeyPrimitiveOperations.<>c__DisplayClass4a.<RemoveAllForeignKeyConstraintsForColumn>b__47(ForeignKeyBuilder fk)
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingConfiguration.CleanupUnmappedArtifacts(DbDatabaseMapping databaseMapping, EntityType table)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingConfiguration.Configure(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest, EntityType entityType, EntityTypeMapping& entityTypeMapping, Boolean isMappingAnyInheritedProperty, Int32 configurationIndex, Int32 configurationCount, IDictionary`2 commonAnnotations)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureTablesAndConditions(EntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, ICollection`1 entitySets, 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.MarkDatabaseInitialized()
   at System.Data.Entity.Database.Initialize(Boolean force)

当上面的context.Database.Initialize(True);代码运行时,在初始化期间,我的调试器将转到一种方法,该方法通过将类应用于DBModelBuilder的400多个实例运行。似乎几乎所有这些都很好,但是有一些实例会导致我看到错误。这是此类实例的示例:

        modelBuilder.Entity<Genesis_Core_Meta.@Workers.Sources.@Worker>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Worker", "Workers");
        })
            .Property(p => p.ContactId)
            .HasDatabaseGeneratedOption(
                System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity
            );

Worker 类是从 Individual 类继承的,而 Individual 类是从 Contact 类继承的。这些类中的每一个都很大,具有许多属性和方法。

我想知道是否有人可以从上面说出我在这些课程中需要寻找的内容,以找出可能导致问题的原因?

我知道仍然有些微不足道,但我希望有人能提供帮助!

0 个答案:

没有答案