我正在使用Web API 5,OData和Entity Framework 6开发ASP.Net MVC5应用程序。我已经创建了存储库并使用Entity Framework Power Tools来生成我的实体模型。我在我的DBContext上关闭了延迟加载和代理生成。下面是我如何在存储库类中编写LINQ查询以返回具有关系的实体;
return repository
.Query(i => i.IsTransaction == true)
.Include(i => i.SubInventory)
.Include(c => c.Contact)
.OrderBy(q => q
.OrderBy(i => i.ItemFullCode))
.Select();
此外,在我的ODataConfig文件中,我已设置;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling
= Newtonsoft.Json.PreserveReferencesHandling.Objects;
我遇到的问题是我的Web API方法JSON响应不包含LINQ查询中包含的关系,而只包含主实体数据。知道我在这里失踪的是什么吗?
以下是我的主要实体的代码。
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; }
}
答案 0 :(得分:1)
如果您正在使用OData查询,那么您可以使用$ expand属性来获取响应中包含的引用。 E.g。
GET http://localhost/odata/Products(1)?$expand=Products/Supplier
将导致加载供应商详细信息,而不仅仅是返回供应商的ID。
{
"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"
}
有关详细信息,请参阅此article。