2

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; }
}
4
  • Could you give us an idea of the what 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. Commented Jul 8, 2015 at 23:52
  • 2
    Are you using code first? Can you display your entities? Commented Jul 8, 2015 at 23:52
  • @evanmcdonnal I've used architecture explained in below link blog.longle.net/2013/05/11/… I've set a break point and even serialized the return object from repository it contains all related objects. It's only on the client side that I don't receive JSON consisting of all objects. Commented Jul 8, 2015 at 23:56
  • @TheVedge I've reverse engineered my entities using Entity Framework Power Tools. Commented Jul 9, 2015 at 0:01

1 Answer 1

1

If you're using OData query then you can use $expand property to get the reference included in your response. E.g.

GET http://localhost/odata/Products(1)?$expand=Products/Supplier

Will result in loading the Supplier details rather than just returning ID of supplier.

{
  "odata.metadata":"http://localhost/odata/$metadata#Categories/@Element",
  "Products":[
    {
      "Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
      "ID":1,"Name":"Hat","Price":"15.00","CategoryId":1,"SupplierId":"CTSO"
    },
    {
      "Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
      "ID":2,"Name":"Scarf","Price":"12.00","CategoryId":1,"SupplierId":"CTSO"
    },{
      "Supplier":{
        "Key":"FBRK","Name":"Fabrikam, Inc."
      },"ID":3,"Name":"Socks","Price":"5.00","CategoryId":1,"SupplierId":"FBRK"
    }
  ],"ID":1,"Name":"Apparel"
}

See this article for details.

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

Comments

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.