1

I am very new to this framework. I let VS scaffold a data model off of my existing database, and I am having issues trying to GET the data correctly from its API 2 controller.

Here is the model:

namespace AngularWebApi.Models
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    public partial class BlockDeveloper
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",        "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public BlockDeveloper()
        {
            this.Reviews = new HashSet<Review>();
            this.Topics = new HashSet<Topic>();
        }

        public int DeveloperId { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }
        public string eMail { get; set; }
        int PostCount { get; set; }
        public int RespondCount { get; set; }
        public int Score { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Review> Reviews { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Topic> Topics { get; set; }
    }
}

Here is the GET I am trying to perform:

namespace AngularWebApi.Controllers
{
    public class BlockDevelopersController : ApiController
    {
         private AngularSiteEntities4 db = new AngularSiteEntities4();

        // GET: api/BlockDevelopers
        public IQueryable<BlockDeveloper> GetBlockDevelopers()
        {
            return db.BlockDevelopers;
        }

        //....more actions...
    }
}

I kept getting the following error:

Type 'System.Data.Entity.DynamicProxies.BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273' with data contract name 'BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I have read quite a few posts and forums and two key solutions pop up: the use of [KnownType(typeof(T))], and Configuration.ProxyCreationEnabled = false;.

I have thrown the KnownType attributes all over the place in my code with no luck - I have absolutely no idea where to do this in an automatically generated ADO.NET entity data model.

I did finally try "Configuration.ProxyCreationEnabled = false;" in my BlockDevelopersController and I no longer get the error. I can finally see my list of BlockDeveloper's - but with one issue, none of the child objects (Reviews, Topics) are being presented (they are empty tags when serialized). I know the data exists, because if I debug and individually loop through each BlockDeveloper's Reviews and Topics I can see it.

I am quite lost as to how to get all of my data. Is there a way to get it if I am using "Configuration.ProxyCreationEnabled = false;"? I seem to be reading that I cannot. How can I use the "[KnownType(typeof(T))]" attribute to correct this (where EXACTLY do these attributes go in an ADO.NET entity data model project?).

Any help would be great, thanks!

6
  • I think the serialization fails because of the virtual properties in your model. When the model gets serialized to JSON, it tries to serialize the collections of reviews & topics too, which probably have references to BlockDeveloper (maybe more?). You have to make sure the serialization doesn't get stuck in an endless loop Commented Nov 23, 2015 at 8:05
  • I normally turn my Entities into POCO classes and send these via Web Api. Commented Nov 23, 2015 at 12:21
  • @AlexanderDerck - you were correct, the entity models did have references to BlockDeveloper (and more) which (if I understood you correctly) would definitely cause a looping issue during serialization. I am not sure why they were automatically setup this way if it could cause an issue like this. I removed the references so that each entity model would have a one-way relationship to its dependencies. Still getting the issue.... Removing tag "virtual" removes the errors, but does not populate the child entities at all. I will keep looking at it, thanks for the suggestion! Commented Nov 23, 2015 at 21:52
  • If the Review and Topic entities no longer have a reference to BlockDeveloper, you should use .Include("Review") in your database call. That should load the child entities of blockdeveloper Commented Nov 23, 2015 at 23:08
  • @AlexanderDerck - Great advise, this allows me to fully return my BlockDeveloper entity through the API! So, I had to remove the unwanted references created when my AngularSite model was created (still not sure why they were created in the first place if it is wrong), I had to add "Configuration.ProxyCreationEnabled = false;" to my context, and I had to finally add ".Include("Reviews").Include("Topics")" to my call to the db. Thanks much! I will give you some time to respond with the answer in case you want to, otherwise I will make it the answer so others might find it easily, thanks again. Commented Nov 24, 2015 at 0:21

1 Answer 1

2

The virtual properties in your model cause an infinite loop when you try to serialize them to JSON, since Review and Topic have references to BlockDeveloper and vice versa. You should use eager loading to get the Review and Topic models from your database with include, instead of trying to lazy-load them.

Sign up to request clarification or add additional context in comments.

3 Comments

Upvoted, once my rep goes to 15 it will count, thanks!
Accepting the answer gives +2
Could have sworn I did, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.