How to configure EF Code First to not map a specific type?
I was getting the following error when runing such this code:
var dbContext = new MyDbContext();
var list = dbContext.Set<TEntity>().ToList();
From the changes I made recently to codes I understood that because I added an event to the base class it causes all the problems:
public PropertyChangedEventHandler PropertyChangedEvent { get; set; }
Applying the NotMapped attribute to the above property my codes now are working again.
Now I want to know if there is anyway to autmatically tell EntityFramework to not Map properties of a specific type(which is not my own type, I could not apply any attribute to a .Net's type).
Exception:
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.E开发者_运维知识库ntity.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()
You can use the Ignore
method of DbModelBuilder
to exclude a type from being mapped.
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<PropertyChangedEventHandler>();
}
}
Use the fluent interface.
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TYPE>().Ignore(c => c.PROPERTY);
}
}
Where TYPE and PROPERTY are relevant to your code, or use the Ignore method on the modelBuilder directly to ignore a type completely
精彩评论