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!
ReviewandTopicentities no longer have a reference toBlockDeveloper, you should use .Include("Review") in your database call. That should load the child entities of blockdeveloper