实体框架5 - 抽象类型“X”没有映射的后代,因此无法映射

时间:2012-09-21 20:51:37

标签: c# entity-framework entity-framework-5 quickfix fix-protocol

尝试操作this object时出现以下错误。有人有任何想法吗? project位于GitHub上,但除非您有FIX服务器,否则您很可能无法运行它。我似乎无法上网这个错误消息。

    System.InvalidOperationException was unhandled by user code
      Message=The abstract type 'QuickFix.Fields.IField' has no mapped descendents and so cannot be mapped. Either remove 'QuickFix.Fields.IField' from the model or add one or more types deriving from 'QuickFix.Fields.IField' to the model. 
      Source=EntityFramework
      StackTrace:
           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.get_InternalContext()
           at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
           at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
           at System.Data.Entity

2 个答案:

答案 0 :(得分:2)

现在这是一个非常有用的错误消息。

  

抽象类型'QuickFix.Fields.IField'没有映射的后代,因此无法映射。从模型中删除“QuickFix.Fields.IField”,或者将从“QuickFix.Fields.IField”派生的一个或多个类型添加到模型中。

显然你有一个抽象类(接口?)IField,你试图从你的上下文中获取这些类的集合。当它是一个抽象类时,你需要有一个或多个派生类(由一个鉴别器列定义),以便EF能够实现查询结果。

如果是界面,则不应映射界面,而应映射实现它的类。

答案 1 :(得分:2)

QuickFix Message对象不是简单的DTO,这使得它们不适合使用任何ORM映射到数据库。 QuickFix为每个FIX字段类型定义了一个不同的IField派生类。这意味着您必须将IField接口映射到数据库以及每个单独的字段类型。

更糟糕的是,QuickFix / N是Java的一个端口,它有许多Java,这使得映射变得非常困难,例如:使用getter / setter方法代替属性。 另一个障碍是每个FIX版本都有一个单独的命名空间,这意味着如果要为所有FIX版本保留消息,则必须将4-5个不同的命名空间映射到有些相同的类。

更好的选择是创建单独的DTO对象,您可以将其映射到数据库并从QuickFix Message对象转换为DTO。幸运的是,QuickFix包含XML格式的各种FIX版本的数据字典,您可以使用它们来生成使用代码生成器的DTO。

为了简化转换,您可以使用基于约定的工具(如AutoMapper)将QuickFix对象转换为DTO,而无需自行编写转换代码。