Cross-table linq query with EF4/POCO
- by Basiclife
Hi All,
I'm new to EF(any version) and POCO. I'm trying to use POCO entities with a generic repository in a "code-first" mode(?)
I've got some POCO Entities (no proxies, no lazy loading, nothing). I have a repository(of T as Entity) which provides me with basic get/getsingle/getfirst functionality which takes a lambda as a parameter (specifically a System.Func(Of T, Boolean))
Now as I'm returning the simplest possible POCO object, none of the relationship parameters work once they've been retrieved from the database (as I would expect).
However, I had assumed (wrongly) that my lambda query passed to the repository would be able to use the links between entities as it would be executed against the DB before the simple POCO entities are generated.
The flow is:
GUI calls:
Public Function GetAllTypesForCategory(ByVal CategoryID As Guid) As IEnumerable(Of ItemType)
Return ItemTypeRepository.Get(Function(x) x.Category.ID = CategoryID)
End Function
Get is defined in Repository(of T as Entity):
Public Function [Get](ByVal Query As System.Func(Of T, Boolean)) As IEnumerable(Of T) Implements Interfaces.IRepository(Of T).Get
Return ObjectSet.Where(Query).ToList()
End Function
The code doesn't error when this method is called but does when I try to use the result set. (This seems to be a lazy loading behaviour so I tried adding the .ToList() to force eager loading - no difference)
I'm using unity/IOC to wire it all up but I believe that's irrelevant to the issue I'm having
NB: Relationships between entities are being configured properly and if I turn on proxies/lazy loading/etc... this all just works. I'm intentionally leaving all that turned off as some calls to the BL will be from a website but some will be via WCF - So I want the simplest possible objects. Also, I don't want a change in an object passed to the UI to be committed to the DB if another BL method calls Commit()
Can someone please either point out how to make this work or explain why it's not possible? All I want to do is make sure the lambda I pass in is performed against the DB before the results are returned
Many thanks.
In case it matters, the container is being populated with everything as shown below:
Container.AddNewExtension(Of EFRepositoryExtension)()
Container.Configure(Of IEFRepositoryExtension)().
WithConnection(ConnectionString).
WithContextLifetime(New HttpContextLifetimeManager(Of IObjectContext)()).
ConfigureEntity(New CategoryConfig(), "Categories").
ConfigureEntity(New ItemConfig()).
...
)