C# EF API Controller Not Loading Many to Many Array

213 views Asked by At

I have the following two classes in a C# ASP.Net application Entity Framework code-first setup

Facility Class;

public class Facility
{
    public int Id { get; set; }
    public string Name {get; set; }
    public ICollection<SubscriberList> SubscriberLists {get; set;}
}

SubscriberList Class;

Public class SubscriberList
{
    public SubscriberList()
    {
        Facilities = new HashSet<Facility>(); //not sure if I need this.
    }
    public int Id { get; set; }
    public int ClientId { get; set; }
    public ICollection<Facility> Facilities { get; set; }
}

With the following Configuration;

public class SubscriberListConfiguration : EntityTypeConfiguration<SubscriberList>
{
    public SubscriberListConfiguration()
    {
        HasMany(w => w.Facilities)
            .WithMany(s => s.SubscriberLists)
            .Map(m =>
            {
                m.ToTable("SubscriberListFacilities");
                m.MapLeftKey("SubscriberListId");
                m.MapRightKey("FacilityId");
            });
    }
}

Now I have the following ApiController

public List<SubscriberList> GetSubscriberLists()
{
    var list = _context.SubscriberLists
        .Include(c => c.Facilities)
        .ToList();
    return list;
 }

When calling the Get request to the /api/SubscriberLists I get the following Json which is missing the "facility name"

[
    {
        "Id": 2,
        "ClientId": 1000001,
        "Facilities": [
            {
                "$id": "1"
            }
        ]
    },
    {
        "Id": 3,
        "ClientId": 1000002,
        "Facilities": [
            {
                "$id": "2"
            },
            {
                "$id": "3"
            }
        ]
    }
]

As you can see it does not return the Facility.Name, only the Facility.Id. I tried by adding the virtual keyword before the ICollection. Tried changing ICollection<> to IList<> as well to List<> Also tried adding the attribute [JsonProperty("Facilities")] on top of the Facilities field. Also tried to iterate the returned list querying the facilities to trigger the loading. Nothing helped so far.

The interesting thing is that in debug mode, I can see everything loaded as expected, I see all the Facility.Name fields populated. I'm afraid the issue here lies at the Json Serializer, but have no clue how to troubleshoot this.

What's my next step?

Thanks in advance

Edit;

Worth to add. When navigating to the Api url in the browser which results in getting XML data instead of Json, I get the following result in the Facilities array.

<Facilities>
    <Facility xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i1"/>
</Facilities>
1

There are 1 answers

9
Nonik On

I think your mapping is a little off, maybe try this

modelBuilder.Entity<Facility>()
.HasMany<SubscriberList>(s => s.Facilities)
.WithMany(c => c.SubscriberLists )
.Map(cs =>
 {
   cs.MapLeftKey("FacilityId");
   cs.MapRightKey("SubscriberListId");
   cs.ToTable("SubscriberListFacilities");
});

Configure Many-to-Many relationship