asp.net web api2 - error when trying to get entity related to entity in webapi 2 -


models:

public class dog     {         [key, databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.identity)]         public guid id { get; set; }          [required]         public string name { get; set; }          public datetime? birthdate { get; set; }          public string color { get; set; }          public string race { get; set; }          public string chipnumber { get; set; }          public byte[] photo { get; set; }            public virtual icollection<record> records { get; set; }          public virtual user user { get; set; }      }  public class record     {         public record()         {             quota = 1;         }          [key, databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.identity)]         public guid id { get; set; }          public datetime? time { get; set; }          [required]         public string name { get; set; }          public string supplier { get; set; }          public double? price { get; set; }          public int quota { get; set; }          public string note { get; set; }            public virtual dog dog { get; set; }          public virtual user user { get; set; }     } 

webapiconfig.cs:

var json = config.formatters.jsonformatter;             json.serializersettings.preservereferenceshandling = newtonsoft.json.preservereferenceshandling.objects;             config.formatters.remove(config.formatters.xmlformatter); 

in webapi calling 'api/dogs' throws exception:

{"$id":"1","message":"an error has occurred.","exceptionmessage":"the 'objectcontent1' type failed serialize response body content type 'application/json; charset=utf-8'.","exceptiontype":"system.invalidoperationexception","stacktrace":null,"innerexception":{"$id":"2","message":"an error has occurred.","exceptionmessage":"error getting value 'records' on 'system.data.entity.dynamicproxies.dog_2ee5cbdae5320b6e973c971e716c24610e64bf4cece05b6719c5876f271ba911'.","exceptiontype":"newtonsoft.json.jsonserializationexception","stacktrace":" @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.calculatepropertyvalues(jsonwriter writer, object value, jsoncontainercontract contract, jsonproperty member, jsonproperty property, jsoncontract& membercontract, object& membervalue)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract 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.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.<writebufferedresponsecontentasync>d__1b.movenext()","innerexception":{"$id":"3","message":"an error has occurred.","exceptionmessage":"an error occurred while executing command definition. see inner exception details.","exceptiontype":"system.data.entity.core.entitycommandexecutionexception","stacktrace":" @ system.data.entity.core.entityclient.internal.entitycommanddefinition.executestorecommands(entitycommand entitycommand, commandbehavior behavior)\r\n @ system.data.entity.core.objects.internal.objectqueryexecutionplan.execute[tresulttype](objectcontext context, objectparametercollection parametervalues)\r\n @ system.data.entity.core.objects.objectquery1.<>c__displayclass7.b__6()\r\n @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess)\r\n @ system.data.entity.core.objects.objectquery1.<>c__displayclass7.b__5()\r\n @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func1 operation)\r\n @ system.data.entity.core.objects.objectquery1.getresults(nullable1 formergeoption)\r\n @ system.data.entity.core.objects.objectquery1.execute(mergeoption mergeoption)\r\n @ system.data.entity.core.objects.dataclasses.entitycollection1.load(list1 collection, mergeoption mergeoption)\r\n @ system.data.entity.core.objects.dataclasses.entitycollection1.load(mergeoption mergeoption)\r\n @ system.data.entity.core.objects.dataclasses.relatedend.load()\r\n @ system.data.entity.core.objects.dataclasses.relatedend.deferredload()\r\n @ system.data.entity.core.objects.internal.lazyloadbehavior.loadproperty[titem](titem propertyvalue, string relationshipname, string targetrolename, boolean mustbenull, object wrapperobject)\r\n @ system.data.entity.core.objects.internal.lazyloadbehavior.<>c__displayclass72.b__1(tproxy proxy, titem item)\r\n @ system.data.entity.dynamicproxies.dog_2ee5cbdae5320b6e973c971e716c24610e64bf4cece05b6719c5876f271ba911.get_records()\r\n @ getrecords(object )\r\n @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)","innerexception":{"$id":"4","message":"an error has occurred.","exceptionmessage":"there open datareader associated command must closed first.","exceptiontype":"system.invalidoperationexception","stacktrace":" @ system.data.sqlclient.sqlinternalconnectiontds.validateconnectionforexecute(sqlcommand command)\r\n @ system.data.sqlclient.sqlconnection.validateconnectionforexecute(string method, sqlcommand command)\r\n @ system.data.sqlclient.sqlcommand.validatecommand(string method, boolean async)\r\n @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method, taskcompletionsource1 completion, int32 timeout, task& task, boolean asyncwrite)\r\n @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method)\r\n @ system.data.sqlclient.sqlcommand.executereader(commandbehavior behavior, string method)\r\n @ system.data.sqlclient.sqlcommand.executedbdatareader(commandbehavior behavior)\r\n @ system.data.common.dbcommand.executereader(commandbehavior behavior)\r\n @ system.data.entity.infrastructure.interception.dbcommanddispatcher.<reader>b__c(dbcommand t, dbcommandinterceptioncontext1 c)\r\n @ system.data.entity.infrastructure.interception.internaldispatcher1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func3 operation, tinterceptioncontext interceptioncontext, action3 executing, action3 executed)\r\n @ system.data.entity.infrastructure.interception.dbcommanddispatcher.reader(dbcommand command, dbcommandinterceptioncontext interceptioncontext)\r\n @ system.data.entity.internal.interceptabledbcommand.executedbdatareader(commandbehavior behavior)\r\n @ system.data.common.dbcommand.executereader(commandbehavior behavior)\r\n @ system.data.entity.core.entityclient.internal.entitycommanddefinition.executestorecommands(entitycommand entitycommand, commandbehavior behavior)"}}}}

solved.

created dto of dog , insted of returning user , records inside dogdto return userid , list of recordids


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -