Linq to Entities (3.5) Children not loading

Hi All,

I'm trying to convert from linq to sql to linq to entities as my team doesn't like the code being tightly coupled (no complaints).

I'm running into issues where I can't load the associated child record.

I manually created an association from the Order table to the Label_Proof table.  It's a one to one.

dm is my instance of the entity

I'm trying to do:

dim lp as Label_Proof = dm.Orders.Include("Label_Proof").Where(Function(o As Order) o.Order_Number = RunCode).FirstOrDefault.Label_Proof

but lp is always coming back nothing.

However if I take out the where clause and just do:


it comes back fine.

I have verified that the label_proof actually exists for the runcode in question.  I can assign the order to a variable and do a manual lookup on label proofs, but that seems cumbersome.  

Note this is EntityFrameworks 1 using .Net 3.5 in VS2008.  

Any and all help greatly appreciated.
LVL 42
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
I found this document which explained how to do it:

Lazy loading wasn't enabled until further versions of the EF.

I'm still not able to get this working correctly unless I made the foreign key the primary key.  Not really sure what's going on but need to abandon this as we're under a deadline.  Thanks for the help.
Bob LearnedCommented:
1) Which version of the Entity Framework are you using (I believe that 5.0 is the latest)?

2) Is the data context set up for lazy loading?
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Hi Learned,

I'm using Visual Studios 2008.  The Web service is for a Handheld using Windows 6.5.3 mobile OS so 3.5 is the only .net version supported (to my knowledge).
I believe I'm using version 1, but is there a way to check?

How do I check that?

Familiar with Linq to SQL but we wanted to get away from being so tightly coupled to the DB.  First time in the EF.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bob LearnedCommented:
If you didn't install anything extra, then you would have version 1, since that was the version that came with 2008.

ADO.NET Entity Framework

That was the version that I never used, because it had so many problems.  Can you describe what you mean by "tightly coupled"?  What are you looking for in a framework?
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
I originally built the web service using LinqToSql.  But after changing some columns around, the program broke because it was looking for references to those columns.  I've noticed in LinqToSQL that even if the column orders are off, things will break.  

So I switched it from LinqToSql to LinqToEntities, rebuilding my DBML into a EDMX.  When I did that however, my linq queries (like the one above) started breaking.  

I'm definitely using the 3.5 version so that would be version 1.

We were trying to keep the handheld and the web service together, so I was told to not upgrade the service part of it to 4.0.  

The good news is that I got it working by implementing the work arounds.  The bad news is I feel it's highly inefficient (essentially making another lookup call rather than loading the child object at the same time).

Does this look okay and does it work in the later versions of EF?

'key to look for of the parent
dim key as integer

' the reference to the EDMX
dim dm as DataBaseModelEntities  

'get the child.
dim c as Child = dm.ParentTable.Include("Child_Table").Where(Function(p As Parent) p.Key = theKey).FirstOrDefault.Child
Bob LearnedCommented:
Do you have the possibility (or the inclination) to look into NHibernate, and AutoMapper?
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
I accept the fact that the first implementation is horrible.  AutoMapper would be objected to due to cost (I'm sure we don't want to expose our schema).

NHibernate looks interesting, and had I'd known about it maybe would have tried, but doesn't really answer the question of how to go about doing this correctly in EF1.  

Did they provide for the ability to do this at all?  How would this be done in EF4?
Bob LearnedCommented:
Children for associations should be loaded, unless lazy loading is used.  I would have thought that the first version of EF would have supported that, but I am not sure that I completely understand your environment.
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
How do I determine if lazy loading is being used?

What else do you need to know about the environment?  Appreciate the help.
Bob LearnedCommented:
I believe that you can get that from the DataContext.ContextOptions:

Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.