-1

I have a webpage that should show a datatable on a getrequest. It works fine on a single table, but if the table has a one to many relationship - it gives error.

        public IEnumerable<Måledata> Get()
    {

            using (SCTHDBEntitiesNew e = new SCTHDBEntitiesNew())
        {
                return e.Måledata.ToList(); 
        }
    }

The error it gives:

{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'BuildBoard' on 'System.Data.Entity.DynamicProxies.Måledata_1EDB55943DE25861C91C9B0997AA34B305E0BBE0570E46E0B9205C41772E4B1A'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" ved Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n ved Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n ved Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n ved.........

Hoping someone can help - longterm ongoing annoying problem

  • Please edit your question to provide your code for your classes to show the relationships between them. Also, please edit the tags to accurately reflect your chosen technologies. For example, [tag:asp.net-web-api] and [tag:entity-framework] if these are indeed what you are using. This should provide the community more information to give better answers – andrensairr Sep 02 '18 at 01:05

3 Answers3

0

The solution for your problem is explained here. Please have a look

Failed to serialize the response in Web API with Json

Web API and EF cause InvalidOperationException

Updated according to your comments -- Please have look here --

EF 6.1 Difference between ProxyCreationEnabled and LazyLoadingEnabled

https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.proxycreationenabled(v=vs.113).aspx

MBB
  • 1,635
  • 3
  • 9
  • 19
  • Hi Mahesh B - thanks for your reply - your last link did the job - I had to add e.Configuration.ProxyCreationEnabled = false; to my controller where I want to return the data content. Thanks again :-) – user3212946 Sep 02 '18 at 15:03
  • Updated answer according to your comments – MBB Sep 02 '18 at 16:33
  • Hi Mahesh - thanks again. Tried to give you a reputation point - but it seems, that I am still to low ranking - need 15 points :-( – user3212946 Sep 03 '18 at 15:05
  • No problem , please accept the answer if that helped :) – MBB Sep 03 '18 at 15:14
0

The answer from Mahesh B had the solution - last link did the job - I had to add e.Configuration.ProxyCreationEnabled = false; to my controller where I want to return the data content.

But I am not sure what the ProxyCreationEnabled does - so if anyone knows ? :-)

0

In Global.asax go the method Application_Start and put the following :

HttpConfiguration config = GlobalConfiguration.Configuration;

config.Formatters.JsonFormatter
            .SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

You have another solution if above is not working :

Instead of use dataContext.TableName.ToList() use dataContext.TableName.ToArray()

Mohammad Ghanem
  • 688
  • 6
  • 20