Entity Framework Include related table when table is empty

.net c# entity-framework entity-framework-6 entity-framework-6.1


I'm reading data from a database into the datasource of a gridview. I can have my Table2 empty. I get an error saying that Table2 doesn't include that column when I attempt to bind columns from it. I examined the information being returned by the query. Table 2 exists, but columns are absent. Simply Count: 0

How can I access related table columns even if they are empty?

return entities.Table1
        .Include("Table3  ")
        .Where(results => results.ID == anotherId)

Table1  one to many  Table2
Table3  one to many  Table1
Table4  one to many  Table2
11/14/2014 9:47:47 AM

Accepted Answer

When utilizing EF Classes include Table1, Table2, and so on.Include(string) a technique is used to cache connected records in a single query. You may eliminateInclude Your query and statements will function similarly. Therefore, you receive count = 0 for Table2 attribute if there are no related records between Table1 and Table2 in your database.

Rerun your query after adding some linked records to Tables 1 and 2 in your database. Table 2 is falseDataSet or DataTable however class.

I believe even that.Include("Table2") is unnecessary if you use.Include("Table2.Table4")

You should be aware that if you want a one-to-many relationship, Table2 must have an FK to Table1.


If you utilize EF, you can gather data from a few tables without having to use joins. But you need well defined FKs. You just usevar collction = table1.Table2 to obtain every Table2 object connected to a Table 1 object.

11/14/2014 10:48:52 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow