We have an issue where we fetch an entity that is several layers deep, then make some changes to the entity, then save it with refetch set to true, but when we do, it doesn't get all of the child entities. Here is the code we use to fetch the entity:
public EmployeeEntity FetchEmployee(EmployeeEntity entityToFetch, bool withChildren)
{
EmployeeEntity employee = new EmployeeEntity(entityToFetch.CompanyId, entityToFetch.EmployeeId);
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.EmployeeEntity);
RelationCollection rulesCollection = new RelationCollection();
rulesCollection.Add(EmployeeEntity.Relations.EmployeeRulesEntityUsingEmployeeRulesId, JoinHint.Left);
if (withChildren)
{
prefetchPath.Add(EmployeeEntity.PrefetchPathDepartment);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeRules, 0, null, rulesCollection);
prefetchPath.Add(EmployeeEntity.PrefetchPathPayclass);
prefetchPath.Add(EmployeeEntity.PrefetchPathFirstRelationshipType);
prefetchPath.Add(EmployeeEntity.PrefetchPathSecondRelationshipType);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeTypes);
IPrefetchPathElement2 environmentPrefetch = prefetchPath.Add(EmployeeEntity.PrefetchPathEnvironment);
environmentPrefetch.SubPath.Add(EnvironmentsEntity.PrefetchPathInAudioCue);
environmentPrefetch.SubPath.Add(EnvironmentsEntity.PrefetchPathOutAudioCue);
environmentPrefetch.SubPath.Add(EnvironmentsEntity.PrefetchPathEnvironmentMessageRange)
.SubPath.Add(EnvironmentMessageRangeEntity.PrefetchPathMessageRanges)
.SubPath.Add(MessageRangesEntity.PrefetchPathAudioCue);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeAllowedDepartments).SubPath.Add(EmployeeAllowedDepartmentsEntity.PrefetchPathDepartment);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeBenefit).SubPath.Add(EmployeeBenefitEntity.PrefetchPathPaycode);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeAssignedTerminals);
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeCustomFields);
}
adapter.FetchEntity(employee, prefetchPath);
return employee;
}
When we fetch the entity using this code, everything works like it should. We have a generic save function that saves an entity that looks like this:
public void SaveEntity(IEntity2 entity, bool refetch)
{
try
{
adapter.SaveEntity(entity, refetch);
}
catch (Exception ex)
{
throw DataExceptionFactory.CreateException(ex);
}
}
The problem is with the Paycode entity. The paycode entity is correct and complete when we fetch it using the first chunk of code. If we make a change and save it, the Paycode entity is null. We noticed that the SQL that is being generated is slightly different for the first fetch:
SELECT [DBA].[EmployeeBenefit].[CompanyID] AS [CompanyId], [DBA].[EmployeeBenefit].[EmployeeID] AS [EmployeeId], [DBA].[EmployeeBenefit].[BenefitPaycodeID] AS [BenefitPaycodeId], [DBA].[EmployeeBenefit].[HoursAllowed], [DBA].[EmployeeBenefit].[HoursTaken], [DBA].[EmployeeBenefit].[StartDate], [DBA].[EmployeeBenefit].[CreateDateTime], [DBA].[EmployeeBenefit].[LastUpdateDateTime], [DBA].[EmployeeBenefit].[SequenceNumber] FROM [DBA].[EmployeeBenefit] WHERE ( ( ( [DBA].[EmployeeBenefit].[CompanyID] = ? AND [DBA].[EmployeeBenefit].[EmployeeID] = ?)))
and the sql that is being generated for the refetch:
SELECT [DBA].[EmployeeBenefit].[CompanyID] AS [CompanyId], [DBA].[EmployeeBenefit].[EmployeeID] AS [EmployeeId], [DBA].[EmployeeBenefit].[BenefitPaycodeID] AS [BenefitPaycodeId], [DBA].[EmployeeBenefit].[HoursAllowed], [DBA].[EmployeeBenefit].[HoursTaken], [DBA].[EmployeeBenefit].[StartDate], [DBA].[EmployeeBenefit].[CreateDateTime], [DBA].[EmployeeBenefit].[LastUpdateDateTime], [DBA].[EmployeeBenefit].[SequenceNumber] FROM [DBA].[EmployeeBenefit] WHERE ( ( [DBA].[EmployeeBenefit].[CompanyID] = ? AND [DBA].[EmployeeBenefit].[EmployeeID] = ? AND [DBA].[EmployeeBenefit].[BenefitPaycodeID] = ?))
Another odd thing is that the other prefetch paths seem to be complete and it is only the Paycode entity that is null (not being refetched).
I am using Version 2.6 and runtime 2.6.09.0616.
Is there a reason why a refetch would not return the correct entity that is fully loaded? Right now we have set the refetch to false and immediately fetch the entity by hand, but aren't sure why we have to do this when the refetch should work.