Entity Framework 4 load and include combine

If I have 3 tables:

Table1, Table2, Table3

And Table3 has a FK for Table2, which has a FK to Table1

Then I load my object like this:

using(Entities entities = new Entities()
{
     Table1 table = entities.Table1.FirstOrDefault();
     table.Table2.Load();
}

How can I eagerly load table3 to table2 because LazyLoading is switched off.

I know I can use Include in the FirstOrDefault statement, but it will generate a much too big join.

ANSWER

using(Entities entities = new Entities())
{
    Table1 table = entities.Table1.FirstOrDefault();
    var table2 = table.Table2.CreateSourceQuery().Include("Table3")
        .Execute(MergeOption.AppendOnly);
    table.Table2.Attach(table2);
}

Answers


How can I eagerly load table3 to table2 because LazyLoading is switched off.

You can try:

using(Entities entities = new Entities())
{
    Table1 table = entities.Table1.FirstOrDefault();
    table.Table2.CreateSourceQuery().Include("Table3")
        .Execute(MergeOption.AppendOnly);
}

I assume that you are using entities derived from EntityObject, not POCOs, i.e. table.Table2 is an EntityCollection<T> or EntityReference<T>. I am not 100% sure if the above code will work as expected.


Need Your Help

How to programmatically generate WSDL for a WCF service?

wcf wsdl

Kindly let me know how to create WSDL for a WCF service programmatically.

I need the category who has the most videos (max() didn't work)

mysql max

I have a MySQL table with categories and video.