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

000*_*000 2 c# ef-code-first entity-framework-4.1

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

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

Era*_*nga 7

您可以使用Ignore方法DbModelBuilder从映射中排除类型.

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Ignore<PropertyChangedEventHandler>();
    }
}
Run Code Online (Sandbox Code Playgroud)


Bet*_*tty 7

使用流畅的界面.

public class Context : DbContext
{       
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<TYPE>().Ignore(c => c.PROPERTY);
  }
}
Run Code Online (Sandbox Code Playgroud)

TYPE和PROPERTY与您的代码相关,或者直接在modelBuilder上使用Ignore方法完全忽略类型