I have a construction that store databases metadata
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; }
}
I would like to retrieve all database related data in one only query with LINQ I can query tables and one child entity from databases
var qry = from d in context.Databases
.Include(x => x.Tables.Select( c => c.Fields))
select d;
But, how can I read two childs from Tables collection? Something like this
var qry = from d in context.Databases
.Include(x => x.Tables.Include(t => t.Fields).Include(t => t.ForeingKeys))
select d;
var qry = from d in context.Databases
.Include("Tables.Fields")
.Include("Tables.ForeingKeys")
select d;
EF will automatically include tables for you and then include those navigation properties in the query.
Another way to achieve the same is
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;
I prefer do not use literals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With