Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ包括多个孩子_C#_Linq_Entity Framework - Fatal编程技术网

C# LINQ包括多个孩子

C# LINQ包括多个孩子,c#,linq,entity-framework,C#,Linq,Entity Framework,我有一个存储数据库元数据的结构 public class Database { public string ConnectionString { get; set; } public virtual ICollection<Table> Tables { get; set; } } public class Table { public string TableName { get; set; } public virtual ICollection

我有一个存储数据库元数据的结构

public class Database {
    public string ConnectionString { get; set; }
    public virtual ICollection<Table> Tables { get; set; }
}

public class Table {
    public string TableName { get; set; }
    public virtual ICollection<ForeingKey> ForeingKeys { get; set; }
    public virtual ICollection<Field> Fields { get; set; }
}
但是,我怎样才能从表格集合中读到两个孩子?像这样的

 var qry = from d in context.Databases
             .Include(x => x.Tables.Include(t => t.Fields).Include(t => t.ForeingKeys))
           select d;

EF将自动为您包括表,然后在查询中包括这些导航属性。

实现相同目的的另一种方法是

        var qry = from d in context.Databases
                      .Include(x => x.Tables)
                      .Include(x => x.Tables.Select(c => c.Fields))
                      .Include(x => x.Tables.Select(f => f.ForeingKeys))
                  select d;

我不希望使用文字。

您的查询会抛出错误“程序集中的名称或类型重复”。我认为这是由于我们将
对象包含了两次。请尝试context.Set()。include(…)。在“备注”部分有更多详细信息。Tables.*需要在后续的Include()中复制,因为否则EF将不知道从哪个属性获取下一个varieble。链接Include()在数据库集上,范围适当。Tacoman667,您的响应是正确的。该错误是由于EF中的错误造成的,但这对性能来说非常糟糕。。您可能希望查看显式加载,这将允许您查询基础集合加上一个不使用字符串文本的集合。它使重构成为一场噩梦。以防万一,您需要它才能使用System.Data.Entity运行

var qry = from d in context.Databases
    .Include("Tables.Fields")
    .Include("Tables.ForeingKeys")
  select d;
        var qry = from d in context.Databases
                      .Include(x => x.Tables)
                      .Include(x => x.Tables.Select(c => c.Fields))
                      .Include(x => x.Tables.Select(f => f.ForeingKeys))
                  select d;