Using the .NET client application, I am trying to send an A object containing a collection of B objects via asp.net web api JSON, create a LocalDB database and save the data. Then select object A.
The application includes 3 projects. The asp.net mvc 4 web api project, the .NET console application and the .NET class library. The asp.net application and the console application reference a class library that includes class definitions for objects A and B.
Class library:
public class Actor { public Actor() { this.Movies = new HashSet<Movie>(); } public int ActorID { get; set; } public string Name { get; set; } public virtual ICollection<Movie> Movies { get; set; } } public class Movie { public Movie() { this.Actors = new HashSet<Actor>(); } public int MovieID { get; set; } public string Name { get; set; } public virtual ICollection<Actor> Actors { get; set; } }
Console Application:
Movie movie = new Movie() { Name = "Dr. No" }; Actor actor = new Actor() { Name = "Sean Connery" }; movie.Actors.Add(actor); using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:3654"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync<Movie>("api/movies", movie).Result; response.EnsureSuccessStatusCode(); response = client.GetAsync("api/movies/1").Result; response.EnsureSuccessStatusCode(); Movie newMovie = response.Content.ReadAsAsync<Movie>().Result; }
asp.net mvc DbContext:
public class MediaContext : DbContext { public MediaContext() { this.Configuration.LazyLoadingEnabled = false; } public DbSet<Movie> Movies { get; set; } public DbSet<Actor> Actors { get; set; } }
Problem # 1: JSON doesn't seem to like a circular link, however, unless I add a collection to both objects, EF5 does not create a MoviesActors table to hold the link.
Problem No. 2: Even if I add a link to the controller when I return this object, it will not return it using the Actors. For instance. I was expecting something like
Movie { MovieID = "1", Name = "???", Actors[] = { 1 } }
But instead, the actors are simply nil.
Update . Here is an exception to self-regulation:
ExceptionMessage = Self-reference loop detected using the type "System.Data.Entity.DynamicProxies.Movie_157D88BDC89E46A7CE4875C2970C7BBFB893972095EFA0745C2261AACC007969". Path '[0] .Actors [0] .Movies'.
I managed to get around this exception using the method. How did I resolve the Json ring reference error? and just turned off the proxy. This solves the problem number 1. However, when I receive films, they still return without Actors, even using Include ("Actors"). I see that the link was created correctly in the staging table in LocalDb.
Update 2
FINALLY figured it out, answer below.
Thank you very much!