|
Ok, so the link above answered my question and while cool, it doesn't allow me to use an Include statement for the filtered collection. So I found the DRC topic "Include and fitler related entities" that offers a suggestion for filtering a navigation collection in a single query to the database. I adapted that example to my model and produced the following: EXAMPLE 1 //Return Standard Work Type and first 10 WorkTypes var myquery = _manager.StandardWorkTypes.Where(e => e.StandardWorkTypeID == standardWorkTypeID)
.Select(swt => new { StandardWorkType = swt, WorkTypes = swt.WorkTypes.Take(10)});
This works, but I'd like to be able to return a related entity from WorkTypes. If I'm not worried about filtering the WorkTypes collection I'd write the query as follows: EXAMPLE 2 //Return Standard Work Type and all Work Types and their associated HealthcareOrganization var query = from entity in _manager.StandardWorkTypes
where entity.StandardWorkTypeID == standardWorkTypeID
select entity;
query = query.Include("WorkTypes.HealthcareOrganization"); QUESTION 1 - Is there a way to query StandardWorkType, take/filter WorkTypes AND include HealthcareOrganization using the syntax from EXAMPLE 1? Before discovering the topic above, I had been issuing two queries, 1 for the StandardWorkType entity and 1 for WorkTypes entity. This worked, but my ViewModel only exposed the StandardWorkType entity and the grid I'm using was bound to {Binding StandardWorkType.WorkTypes}. So even though I had loaded the data I wanted in a separate query, DevForce went back to the database because it was the first time I had accessed the WorkTypes navigation property for my entity. I then discovered the DRC topic "Navigation properties and data retrieval" and set a new EntityReferenceStrategy using DomainModel.StandardWorkType.PropertyMetadata.WorkTypes.ReferenceStrategy = new EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.PreserveChanges);
This works as expected and is just beautiful. I also get the same results if I use DomainModel.StandardWorkType.PropertyMetadata.WorkTypes.GetEntityReference(standardWorkType).IsLoaded = true;
QUESTION 2 - Do you see any red flags here? If I want to set the EntityReferenceStrategy application wide using the first method, should I do that on application startup? The ERS is app wide across all Entity Mangers, right? Thanks for your help.
|