I'm developing an ASP.Net MVC5 application using Web API 5, OData and Entity Framework 6. I've created Repositories and used Entity Framework Power Tools to generate my entity models. I've turned OFF lazy loading and proxy generation on my DBContext. Below is how i'm writing my LINQ queries in repository classes to return entities with relationships;
return repository
.Query(i => i.IsTransaction == true)
.Include(i => i.SubInventory)
.Include(c => c.Contact)
.OrderBy(q => q
.OrderBy(i => i.ItemFullCode))
.Select();
Further in my ODataConfig file I've set;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling
= Newtonsoft.Json.PreserveReferencesHandling.Objects;
The issue that I'm struggling with is that my Web API method JSON response does NOT contain relationships which are Included in LINQ query but just the main entity data. Any idea what I'm missing here?
Below is the code of my main entity.
public partial class Item : Entity
{
public Item()
{
this.DocumentDatas = new List<DocumentData>();
this.ItemColorSizes = new List<ItemColorSize>();
this.ItemPrices = new List<ItemPrice>();
this.Items1 = new List<Item>();
this.ItemStocks = new List<ItemStock>();
this.SeasonalSaleDetails = new List<SeasonalSaleDetail>();
}
public string ItemFullCode { get; set; }
public string ItemCode { get; set; }
public string ItemName { get; set; }
public string ItemShortCode { get; set; }
public string LevelItemFullCode { get; set; }
public string SupplierCode { get; set; }
public string SubInvCode { get; set; }
public Nullable<decimal> PurchasePrice { get; set; }
public Nullable<decimal> SalePrice { get; set; }
public Nullable<System.DateTime> ArrivalDate { get; set; }
public string RefCode { get; set; }
public Nullable<decimal> TColumn { get; set; }
public Nullable<bool> TColumnByAmt { get; set; }
public Nullable<bool> IsGiftItem { get; set; }
public Nullable<bool> IsTransaction { get; set; }
public Nullable<bool> IsActive { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }
public Contact Contact { get; set; }
public ICollection<DocumentData> DocumentDatas { get; set; }
public ICollection<ItemColorSize> ItemColorSizes { get; set; }
public ICollection<ItemPrice> ItemPrices { get; set; }
public ICollection<Item> Items1 { get; set; }
public Item Item1 { get; set; }
public SubInventory SubInventory { get; set; }
public ICollection<ItemStock> ItemStocks { get; set; }
public ICollection<SeasonalSaleDetail> SeasonalSaleDetails { get; set; }
}
repository's definition looks like? Also could you confirm that in code, when you set a break point after that return, that all the expected related objects have been deserialized. My suspicion is that feature doesn't apply to the entity relationships. My experience with it was that if you had a recursive structure, you needed it. Like a type that referenced an array of the same type.