小编000*_*000的帖子

如何配置EF代码优先不映射特定类型?

运行这样的代码时出现以下错误:

var dbContext = new MyDbContext();
var list = dbContext.Set<TEntity>().ToList();
Run Code Online (Sandbox Code Playgroud)

从我最近做的更改到代码我理解,因为我向基类添加了一个事件,它会导致所有问题:

public PropertyChangedEventHandler PropertyChangedEvent { get; set; }
Run Code Online (Sandbox Code Playgroud)

将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 …
Run Code Online (Sandbox Code Playgroud)

c# ef-code-first entity-framework-4.1

2
推荐指数
2
解决办法
6681
查看次数

标签 统计

c# ×1

ef-code-first ×1

entity-framework-4.1 ×1