2014-01-09 62 views
0

我试图选择数据库aswel中的所有项目作为使用实体框架的所有依赖项。从数据库中选择项目加上所有依赖项

执行选择时出现以下错误 指定的包含路径无效。 EntityType'RequestModel.RequestType'不声明名为'FieldDefinitions,ListItems'的导航属性。

这是我的代码,林肯定它的东西简单即时通讯失踪。

var reqs = new List<RequestType>(); 

    using (var db = new ITISS_RequestEntities()) 
    { 

     reqs = db.RequestTypes.Include("FieldDefinitions,ListItems").ToList(); 
    } 

这里,你使用虚拟ICollections所以一切都应该是可以通过延迟加载反正我的模型

public partial class RequestType 
    { 
     public RequestType() 
     { 
      this.Requests = new HashSet<Request>(); 
      this.FieldDefinitions = new HashSet<FieldDefinition>(); 
     } 

     public int RequestType1 { get; set; } 
     public string TypeDescription { get; set; } 

     public virtual ICollection<Request> Requests { get; set; } 
     public virtual ICollection<FieldDefinition> FieldDefinitions { get; set; } 
    } 

    public partial class FieldDefinition 
    { 
     public FieldDefinition() 
     { 
      this.EntryDefinitions = new HashSet<EntryDefinition>(); 
      this.ListItems = new HashSet<ListItem>(); 
      this.RequestTypes = new HashSet<RequestType>(); 
     } 

     public int RequestField { get; set; } 
     public string FieldName { get; set; } 
     public string FieldReference { get; set; } 
     public string ControlType { get; set; } 

     public virtual ICollection<EntryDefinition> EntryDefinitions { get; set; } 
     public virtual ICollection<ListItem> ListItems { get; set; } 
     public virtual ICollection<RequestType> RequestTypes { get; set; } 
    } 

    public partial class ListItem 
    { 
     public ListItem() 
     { 
      this.FieldDefinitions = new HashSet<FieldDefinition>(); 
     } 

     public int ListItemID { get; set; } 
     public string ItemName { get; set; } 

     public virtual ICollection<FieldDefinition> FieldDefinitions { get; set; } 
    } 

回答

0

首先的。

如果你真的想迫使整个树的单个负载,然后改变这种

reqs = db.RequestTypes.Include("FieldDefinitions,ListItems").ToList(); 

这个

reqs = db.RequestTypes.Include("FieldDefinitions").Select("ListItems").ToList(); 

此外,如果添加

using System.Data.Entity 

到您可以使用包含.....的强类型扩展方法,因此.....您可以使用.....

reqs = db.RequestTypes.Include(rt => rt.FieldDefinitions).Select(fd => fd.ListItems).ToList(); 

其实this是实体框架一个真正有用的页面查询

+0

只是为了让事情更清楚小有一点,使用实体框架5.实体框架6.获得的类型,你提到的选项是林“System.Linq.Queryable.Select (System.Linq.IQueryable ,System.Linq.Expressions.Expression >)'的参数无法从使用情况中推断出来。尝试明确指定类型参数。 – Ernie

+0

你可以在EF5中使用它们,如果你在顶部包含“using”的话 - 正如我指定的那样:) –

+0

感谢你,因为RequestType不包含ListItems的定义 – Ernie

相关问题