我有一個控制器:寧靜的C#頁面無法顯示正確
public IEnumerable<Food> Get()
{
FoodDiaryContext cs = new FoodDiaryContext();
var foodquery = cs.Foods.OrderByDescending(c => c.Description).ToList();
return foodquery;
}
它應該顯示:
{
id:1
description:food
measure:[]
}
但我得到一個錯誤:
Type 'System.Data.Entity.DynamicProxies.Food_219DB877F13A4776536ADFD2AC86DB1FDBF0E8887DB7E9D658EFE7D9462C3BD5' with data contract name 'Food_219DB877F13A4776536ADFD2AC86DB1FDBF0E8887DB7E9D658EFE7D9462C3BD5: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies ' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
public class Food
{
public Food()
{
Measures = new List<Measure>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<Measure> Measures { get; set; }
}
public class Measure
{
public int Id { get; set; }
public string Description { get; set; }
public double Calories { get; set; }
public double Fats { get; set; }
public double Protein { get; set; }
public double Carbohydrates { get; set; }
public virtual Food Food { get; set; }
}
燦任何人都請告訴我我做錯了什麼?
謝謝
嘗試在您的數據庫上下文中禁用代理創建,它不是ASP.NET特定的錯誤,也嘗試更新您的上下文模型。 – narekye
爲什麼要將域模型返回給客戶端,有什麼特別的理由嗎? –