How to configure EF Code First to not map a specific type?

核能气质少年 提交于 2019-12-11 03:21:02

问题


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.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()

回答1:


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>();
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/7642205/how-to-configure-ef-code-first-to-not-map-a-specific-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!