【发布时间】:2011-11-30 07:54:13
【问题描述】:
运行此类代码时出现以下错误:
var dbContext = new MyDbContext();
var list = dbContext.Set<TEntity>().ToList();
从我最近对代码所做的更改中,我了解到因为我在基类中添加了一个事件,所以会导致所有问题:
public PropertyChangedEventHandler PropertyChangedEvent { get; set; }
将 NotMapped 属性应用于上述属性,我的代码现在又可以正常工作了。
现在我想知道是否有任何方法可以自动告诉 EntityFramework 不映射特定类型的属性(这不是我自己的类型,我无法将任何属性应用于 .Net 的类型)。
例外:
Sequence does not contains any element.
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(DbDatabaseMapping databaseMapping, EdmEntityType entityType)
at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(EdmAssociationType associationType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.Generate(EdmAssociationType associationType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAssociationTypes(EdmModel model, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel model)
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel model, 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.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.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
【问题讨论】:
-
我知道这已经很老了,但是您知道如何确定导致此异常的属性吗?我正在尝试从现有的大型数据层使用 Code First,我看到了这条消息,但我不知道是什么导致了问题。
-
通过查看我最近对代码所做的更改,我确实找到了罪魁祸首。这是一个异常,它是 EF 的错误,而不是故意的异常。所以不知道在什么情况下会出现类似的异常。
-
谢谢。到目前为止,我正在经历一个痛苦的过程,从我的实体结构的底部开始,一次一行地注释掉代码,以找出有效的和失败的。有时这些消息很有帮助,有时我会遇到这样的异常。
-
如果您看到完全相同的异常(First 之后最里面的方法是 GetEntityTypeMappingInHierarchy),那么您必须找到 EF 尝试映射的 entityType。因此,我建议使用 DbModelBuilder.Ignore 方法一次忽略一种类型。
标签: c# entity-framework-4.1 ef-code-first