• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

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:

 dm.Orders.Include("Label_Proof").FirstOrDefault.Label_Proof

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.
0
Kyle Abrahams
Asked:
Kyle Abrahams
  • 6
  • 5
2 Solutions
 
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?
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Hi Learned,

1)
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?


2)
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.
0
 
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
http://en.wikipedia.org/wiki/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?
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
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
0
 
Bob LearnedCommented:
Do you have the possibility (or the inclination) to look into NHibernate, and AutoMapper?
0
 
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?
0
 
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.
0
 
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.
0
 
Bob LearnedCommented:
I believe that you can get that from the DataContext.ContextOptions:

dataContext.ContextOptions.LazyLoadingEnabled
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
I found this document which explained how to do it:

http://blogs.msdn.com/b/adonet/archive/2007/05/10/mapping-101-part-2-association-mapping.aspx

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.
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks for the help.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now