![](/img/trans.png)
[英]Is it possible to detect when a reference property has changed on an entity in EF6?
[英]EF does not give complete json when it has one entity that has one navigation property like itself
我在asp.net核心中有一個項目,在這個項目中,我有兩個實體。 第一實體是產品
public class Product
{
public int id { get; set;}
public string Name { get; set; }
public virtual Brand Brand { get; set; }
}
我的第二堂課是
public class Brand
{
public Brand()
{
Products = new List<Products>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Product> Products { get; set; }
}
我有一個像這樣的APIController
public class APIController : ControllerBase
{
public object GetProducts()
{
return decorDB.Products
.Include(p => p.Brand);
}
}
我得到的是一個像這樣的json
[
{
"id":1,
"name":"iPhone",
"brand":
{
"id":1,
"name":"Apple",
"products":[
如您所見,它還不完整,我認為EF會將其斷開以防止循環。 我想要這樣的東西
[
{
"id":1,
"name":"iPhone",
"brand":
{
"id":1,
"name":"Apple",
"products":[]
}
}
]
我不知道該怎么辦!
免責聲明:未經測試,但希望發表評論。
我建議進行以下更改:
首先嘗試更改操作結果簽名:
public IEnumerable<Product> GetProducts()
下一步:嘗試在啟動時顯式禁用往返:
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.