如何配置EF代码首先不映射特定类型? [英] How to configure EF Code First to not map a specific type?

查看:87
本文介绍了如何配置EF代码首先不映射特定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var dbContext = new MyDbContext(); 
var list = dbContext.Set< TEntity>()。ToList();

从我最近修改的代码中我明白,因为我向基类添加了一个事件导致所有问题:

  public PropertyChangedEventHandler PropertyChangedEvent {get;组; 

将NotMapped属性应用到上述属性,我的代码现在正在重新工作。



现在我想知道是否有自动地告诉EntityFramework不映射特定类型的属性(这不是我自己的类型,我不能应用任何属性到.Net的类型)



异常:

 序列不包含任何元素。 

在System.Linq.Enumerable.First [TSource](IEnumerable`1源,Func`2谓词)
在System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy (DbDatabaseMapping databaseMapping,EdmEntityType entityType)
在System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(EdmAssociationType关联类型,DbDatabaseMapping数据库映射)
在System.Data.Entity.ModelConfiguration.Edm.Services .AssociationTypeMappingGenerator.Generate(EdmAssociationType associationType,DbDatabaseMapping databaseMapping)
在System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAssociationTypes(EdmModel模型,DbDatabaseMapping数据库映射)
在System.Data.Entity.ModelConfiguration .Edm.Services.DatabaseMappingGenerator.Generate(EdmModel模型)
在System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.G enerateDatabaseMapping(EdmModel模型,DbProviderManifest providerManifest)
在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
在System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
在System.Data.Entity。 Internal.LazyInternalContext.InitializeContext()
在System.Data.Entity.Internal.InternalContext.Initialize()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
在System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
在System.Data.Entity。 Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable< TResult> .GetEnumerator()


解决方案

您可以使用 Ignore 方法 DbModelBuilder 以排除类型被映射。

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


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

解决方案

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

这篇关于如何配置EF代码首先不映射特定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆