C# Web Api+;实体框架未正确加载数据

C# Web Api+;实体框架未正确加载数据,c#,rest,entity-framework-4.1,asp.net-web-api,json.net,C#,Rest,Entity Framework 4.1,Asp.net Web Api,Json.net,我使用ASP.NETWebAPI构建了一个小型服务。我的域类如下所示: public class Drink : IEntity { public Drink() { Ingridients = new List<Ingredient>(); } public int Id { get; set; } public string Name { get; set; } public ICollection<Ing

我使用ASP.NETWebAPI构建了一个小型服务。我的域类如下所示:

 public class Drink : IEntity
{
    public Drink()
    {
        Ingridients = new List<Ingredient>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Ingredient> Ingridients { get; set; }
    public string Approach { get; set; }
}

public class Ingredient : IEntity
{
    public Ingredient()
    {
        Drinks = new List<Drink>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Drink> Drinks { get; set; }
}
public IEnumerable<T> GetAll(){return _dbSet;}
    public IEnumerable<Drink> GetAllDrinks()
    {
        return _unitOfWork.Drinks.GetAll();
    }

如您所见,Ingridents数组为空。为什么会这样?

您是否首先使用实体框架代码

如果是,则必须将导航属性标记为
virtual

public class Drink : IEntity
{
    public Drink()
    {
        Ingridients = new List<Ingredient>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Ingredient> Ingridients { get; set; }
    public string Approach { get; set; }
}

public class Ingredient : IEntity
{
    public Ingredient()
    {
        Drinks = new List<Drink>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Drink> Drinks { get; set; }
}
public-class-Drink:IEntity
{
公共饮料
{
Ingridients=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection Ingridients{get;set;}
公共字符串方法{get;set;}
}
公共类成分:灵活性
{
公共成分()
{
饮料=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection{get;set;}
}
您可以尝试在
包含
时使用即时加载:

扩展您的
GetAll
方法以允许快速加载:

//...
using System.Data.Entity;
//...

public IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includes)
{
    IQueryable<T> query = _dbSet;
    if (includes != null)
    {
        foreach (var include in includes)
            query = query.Include(include);
    }
    return query;
}
/。。。
使用System.Data.Entity;
//...
公共IEnumerable GetAll(参数表达式[]包含)
{
IQueryable查询=_dbSet;
如果(包括!=null)
{
foreach(包含在包含中的var)
query=query.Include(包含);
}
返回查询;
}
然后在控制器中使用它,如下所示:

public IEnumerable<Drink> GetAllDrinks()
{
    return _unitOfWork.Drinks.GetAll(d => d.Ingredients);
}
public IEnumerable GetAllDiverses()
{
return _unitOfWork.Drinks.GetAll(d=>d.components);
}

是的,我正在使用CF。将导航属性标记为虚拟属性没有帮助。我仍然得到空数组。我添加了json.net标记,因为这个问题实际上是“为什么json.net不能正确序列化我的EF图。”Web api是这个问题的附带问题。看看这个问题吧。。也可以看看这个博客,可能会有帮助:看起来很有趣,拉杰什。我会尽快看一看,现在我得到了一个不同的错误..就像发生了一个错误一样。'ObjectContent'1'类型未能序列化内容类型'application/xml'的响应正文;字符集=utf-8'。System.InvalidOperationException发生错误。“Gudo.Core.Model.component”类型的对象图包含循环,如果禁用引用跟踪,则无法序列化。此错误发生在我应用Slauma所做的更改之后suggested@IAmSharp:搜索错误或提出新问题。网上有很多关于这个问题的参考资料。例如:或者(对于JSON.NET),该错误与未加载成分的原始问题无关。好的。我将添加一个新问题
public IEnumerable<Drink> GetAllDrinks()
{
    return _unitOfWork.Drinks.GetAll(d => d.Ingredients);
}