.net - EF6 Automapper Inheritance/Abstract Class Errors -


i'm building .net web api using entity framework 6 automapper map entities dtos. when try introduce tph (table per hierarchy) abstract base class, long , useless (to me @ least) error. error below, i'm not sure actual error is. i've tried googling various sections of no avail.

this mapping:

        mapper.createmap<parentclass, parentclassdto>()             .include<classa, classadto>()             .include<classb, classbdto>();          mapper.createmap<classa, classadto>();         mapper.createmap<classb, classbdto>(); 

if remove abstract parentclass, error goes away automapper not use child dto classes, uses parentclassdto. if remove mapping , return entities through api, there no errors , works expected.

i don't know start error below, how start debugging (web api running in debug mode still outputs error screen), or responsible error. i've simplified code as possible , tried other various things, can't seem around issue.

this specific mapping collection within dto class, creating return iqueryable<anotherclassdto> web api:

return db.anotherclass.project().to<anotherclassdto>(); 

the anotherclassdto (a separate class contains abstract collection) setup this:

public class anotherclassdto {     public ienumerable<parentclassdto> elements; } 

error message:

> {"message":"an error has occurred.","exceptionmessage":"the > 'objectcontent`1' type failed serialize response body > content type 'text/html; > charset=utf-8'.","exceptiontype":"system.invalidoperationexception","stacktrace":null,"innerexception":{"message":"an > error has occurred.","exceptionmessage":"exception has been thrown > target of > invocation.","exceptiontype":"system.reflection.targetinvocationexception","stacktrace":" > @ system.runtimemethodhandle.invokemethod(object target, object[] > arguments, signature sig, boolean constructor)\r\n @ > system.reflection.runtimemethodinfo.unsafeinvokeinternal(object obj, > object[] parameters, object[] arguments)\r\n @ > system.reflection.runtimemethodinfo.invoke(object obj, bindingflags > invokeattr, binder binder, object[] parameters, cultureinfo > culture)\r\n @ system.reflection.methodbase.invoke(object obj, > object[] parameters)\r\n @ > system.data.entity.core.common.internal.materialization.translator.translatecolumnmap(translator > translator, type elementtype, columnmap columnmap, metadataworkspace > workspace, spanindex spanindex, mergeoption mergeoption, boolean > streaming, boolean valuelayer)\r\n @ > system.data.entity.core.objects.internal.objectqueryexecutionplanfactory.prepare(objectcontext > context, dbquerycommandtree tree, type elementtype, mergeoption > mergeoption, boolean streaming, span span, ienumerable`1 > compiledqueryparameters, aliasgenerator aliasgenerator)\r\n @ > system.data.entity.core.objects.elinq.elinqquerystate.getexecutionplan(nullable`1 > formergeoption)\r\n @ > system.data.entity.core.objects.objectquery`1.<>c__displayclass7.b__6()\r\n > @ > system.data.entity.core.objects.objectcontext.executeintransaction[t](func`1 > func, idbexecutionstrategy executionstrategy, boolean > startlocaltransaction, boolean releaseconnectiononsuccess)\r\n @ > system.data.entity.core.objects.objectquery`1.<>c__displayclass7.b__5()\r\n > @ > system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 > operation)\r\n @ > system.data.entity.core.objects.objectquery`1.getresults(nullable`1 > formergeoption)\r\n @ > system.data.entity.core.objects.objectquery`1..getenumerator>b__0()\r\n > @ system.data.entity.internal.lazyenumerator`1.movenext()\r\n @ > newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter > writer, ienumerable values, jsonarraycontract contract, jsonproperty > member, jsoncontainercontract collectioncontract, jsonproperty > containerproperty)\r\n @ > newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter > writer, object value, jsoncontract valuecontract, jsonproperty member, > jsoncontainercontract containercontract, jsonproperty > containerproperty)\r\n @ > newtonsoft.json.serialization.jsonserializerinternalwriter.serialize(jsonwriter > jsonwriter, object value, type objecttype)\r\n @ > newtonsoft.json.jsonserializer.serializeinternal(jsonwriter > jsonwriter, object value, type objecttype)\r\n @ > newtonsoft.json.jsonserializer.serialize(jsonwriter jsonwriter, object > value)\r\n @ > system.net.http.formatting.basejsonmediatypeformatter.writetostream(type > type, object value, stream writestream, encoding > effectiveencoding)\r\n @ > system.net.http.formatting.jsonmediatypeformatter.writetostream(type > type, object value, stream writestream, encoding > effectiveencoding)\r\n @ > system.net.http.formatting.basejsonmediatypeformatter.writetostream(type > type, object value, stream writestream, httpcontent content)\r\n @ > system.net.http.formatting.basejsonmediatypeformatter.writetostreamasync(type > type, object value, stream writestream, httpcontent content, > transportcontext transportcontext, cancellationtoken > cancellationtoken)\r\n--- end of stack trace previous location > exception thrown ---\r\n @ > system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task > task)\r\n @ > system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task > task)\r\n @ > system.runtime.compilerservices.taskawaiter.getresult()\r\n @ > system.web.http.webhost.httpcontrollerhandler.d__1b.movenext()","innerexception":{"message":"an > error has occurred.","exceptionmessage":"exception has been thrown > target of > invocation.","exceptiontype":"system.reflection.targetinvocationexception","stacktrace":" > @ system.runtimemethodhandle.invokemethod(object target, object[] > arguments, signature sig, boolean constructor)\r\n @ > system.reflection.runtimeconstructorinfo.invoke(bindingflags > invokeattr, binder binder, object[] parameters, cultureinfo > culture)\r\n @ system.runtimetype.createinstanceimpl(bindingflags > bindingattr, binder binder, object[] args, cultureinfo culture, > object[] activationattributes, stackcrawlmark& stackmark)\r\n @ > system.activator.createinstance(type type, bindingflags bindingattr, > binder binder, object[] args, cultureinfo culture, object[] > activationattributes)\r\n @ system.activator.createinstance(type > type, object[] args)\r\n @ > system.data.entity.core.common.internal.materialization.coordinatorscratchpad.compile()\r\n > @ > system.data.entity.core.common.internal.materialization.coordinatorscratchpad.compile()\r\n > @ > system.data.entity.core.common.internal.materialization.coordinatorscratchpad.compile()\r\n > @ > system.data.entity.core.common.internal.materialization.translator.translatecolumnmap[t](columnmap > columnmap, metadataworkspace workspace, spanindex spanindex, > mergeoption mergeoption, boolean streaming, boolean > valuelayer)","innerexception":{"message":"an error has > occurred.","exceptionmessage":"instances of abstract classes cannot > created.","exceptiontype":"system.invalidoperationexception","stacktrace":" > @ > system.runtime.compilerservices.runtimehelpers._compilemethod(iruntimemethodinfo > method)\r\n @ > system.reflection.emit.dynamicmethod.createdelegate(type delegatetype, > object target)\r\n @ > system.linq.expressions.compiler.lambdacompiler.createdelegate()\r\n > @ > system.linq.expressions.compiler.lambdacompiler.compile(lambdaexpression > lambda, debuginfogenerator debuginfogenerator)\r\n @ > system.linq.expressions.expression`1.compile()\r\n @ > system.data.entity.core.common.internal.materialization.coordinatorfactory`1..ctor(int32 > depth, int32 stateslot, expression`1 hasdata, expression`1 setkeys, > expression`1 checkkeys, coordinatorfactory[] nestedcoordinators, > expression`1 element, expression`1 wrappedelement, expression`1 > elementwitherrorhandling, expression`1 initializecollection, > recordstatefactory[] recordstatefactories)\r\n @ > system.data.entity.core.common.internal.materialization.coordinatorfactory`1..ctor(int32 > depth, int32 stateslot, expression hasdata, expression setkeys, > expression checkkeys, coordinatorfactory[] nestedcoordinators, > expression element, expression elementwitherrorhandling, expression > initializecollection, recordstatefactory[] recordstatefactories)"}}}} 

2 problems

  1. the error message states can't map abstract class
  2. you're working queryable extensions project().to<t>() seems suffer, in case, limitations due linq provider, mentionned here

=> make base class concrete

=> map ienumerable<t> instead of iqueryable<t>, without using queryable extensions.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -