0
votes

I have a controller:

public IEnumerable<Food> Get()
{
    FoodDiaryContext cs = new FoodDiaryContext();
    var foodquery = cs.Foods.OrderByDescending(c => c.Description).ToList();

    return foodquery;
}

It should display:

{
    id:1
    description:food
    measure:[]
}

But I am getting an error:

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; }
}

Can anyone please tell me what I have done wrong?

Thanks

1
Try disabling the proxy creation on your db context, its not an ASP.NET specific error, and also try to update your context model. - narekye
Is there any particular reason why you want to return domain model back to the client? - Husein Roncevic

1 Answers

0
votes

Thanks for the help. I did want you suggested and it works.

public class FoodDiaryContext:DbContext
    {
        public FoodDiaryContext()
        {
            this.Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<Food> Foods { get; set; }
        public DbSet<Measure> Measures { get; set; }
    }