I am doing what seems like what is probably a pretty common task with OpenAccedd/Data Access and that is supplying a WebAPI web service with data to fulfill it's requests. I am running into the typical issue of the entities getting serialized and the graphs getting serialized out as well.
The suggestions I have read have been to explicitly set the FetchStrategy on the context when the query is executed, and that this would limit the navigation properties that were expanded when serialized. That, in addition to creating a detached copy should resolve the issue. I must be doing something wrong however because I have had no luck doing these things.
// WEB API GET METHOD
CatalogAPIController : ApiController
List<DeliverableType> data = CatalogAdminAppRepository.GetDeliverableTypeList();
// Package the data in a Custom Response container
CustomHttpResponse<DeliverableType> response = CustomResponse<DeliverableType>.Create(data);
// Package the container into a HttpResponseMessage to be returned
ret = Request.CreateResponse<CustomHttpResponse<DeliverableType>>(HttpStatusCode.OK, response);
CustomHttpResponse<Exception> response = CustomHttpResponse<Exception>.CreateForException(ex);
ret = Request.CreateResponse<CustomHttpResponse<Exception>>(HttpStatusCode.InternalServerError, response);
// The Repository code which retrieves the Entity from the context.
CatalogAdminAppRepository : ICatalogAdminAppRepository
DeliverableType GetDeliverableTypeByID(Int64 typeID)
DeliverableType ret =
(CatalogAdminDbRepository<DeliverableType> repo =
// define a fetch strategy to get only the navigation properties we are
// interested in, limiting the depth to only 1 layer. This should prevent
// the serialization of any navigation properties associated with the
// Deliverables and Type properties.
FetchStrategy fetch =
fetch.LoadWith<DeliverableType>(e => e.Deliverables);
fetch.LoadWith<Deliverable>(e => e.Type);
fetch.MaxFetchDepth = 1;
// Associate the fetch strategy to the Context being used to
// retrieve the entity collection
repo.DbContext.FetchStrategy = fetch;
ret = repo.GetEntityByID(typeID);
// also assign it to the CreateDetachedCopy method
ret = repo.DbContext.CreateDetachedCopy<DeliverableType>(ret, fetch);
So I have done the following things to prevent the loading of navigation properties when they serialize ...
- Disposed of the Context
- Defined an explicit FetchStrategy
- Created a detached copy
After all of this everything is STILL being loaded. I must be missing something. Can you see what I am overlooking?
An aside question I have is ... If the context is where all the connection information is stored, how is it possible for these navigation properties to load once the Context has been disposed of?