我今天将EF更新为5到6,我按照以下步骤从MSDN开始:
http://msdn.microsoft.com/en-us/data/upgradeef6.aspx
现在的问题是,当我查询任何存储库时,例如:
mapEntitiesContext.ContextParameterScreens
.Where(p => p.OwningTenantId == IdentityHelper.IdPrv.TenantId)
.Select(p => new
{
p.ParameterScreenDefinitionId,
p.MedicalContextId
})
.FirstOrDefault();
我收到错误:
序列包含多个元素。
我从未遇到过EF 5.0的这个问题。
Stacktrace
System.InvalidOperationException: Sequence contains more than one element
Result StackTrace:
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass281.<IndexesEqual>b__27e(String c)
at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(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__DisplayClass272.<FindAddedIndexes>b__26e(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.Set`1.Add(TElement value)
at System.Linq.Enumerable.<ExceptIterator>d__99`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.Internal.InternalContext.ModelMatches(VersionedModel model)
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata, DatabaseExistenceState existenceState)
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata, DatabaseExistenceState existenceState)
at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata, DatabaseExistenceState existenceState)
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
答案 0 :(得分:3)
经过大量的测试和调试后,我终于找到了一个“解决方案”。
我从一个新项目开始,以“add-migration InitialCreate -verbose”开始,而使用EF 6.0,此方法创建一个名为“InitialSchema”的新文件。
当我开始当前的迁移(一年多以前)时,初始文件名为:Initial。 只需添加名为“InitialSchema”的空类即可修复错误。
public partial class InitialSchema : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
当我启动我的应用程序并创建了我的DbContext时,EF正在崩溃
希望它也有助于其他人:)
答案 1 :(得分:2)
您发布的堆栈跟踪表明它不是您的查询的问题,而是基础Entity Framework基础结构。它尝试在对数据库执行任何操作之前初始化数据库(即您的查询),并且这样做的一部分将其现有模式与当前模式进行比较。在比较自上次在数据库上运行EF以来添加或删除列的某些表的索引时,它会失败 - 引发的SingleOrDefault
采用IndexesEqual
方法EdmModelDiffer
。所以你可以: