Thanks for the code. I added to it a litle (and translated to VB.NET) and got the following:
Dim entityToValidate As My<[CurrentEntityName]>Entity = CType(containingEntity, My<[CurrentEntityName]>Entity)
Dim adapter As New DatabaseSpecific.DataAccessAdapter
<[Foreach RelatedEntity ManyToOne ]>
<[ Foreach RelationField ]>
<[If IsForeignKey]>
If entityToValidate.Fields(CType(<[CurrentEntityName]>FieldIndex.<[RelationFieldName]>, Integer)).IsChanged Then
Dim relationInfo As IRelationPredicateBucket = entityToValidate.GetRelationInfo<[MappedFieldNameRelation]>()
' fetch entity here
Dim collectionToFetch as New HelperClasses.EntityCollection(New FactoryClasses.My<[RelatedEntityName]>EntityFactory)
adapter.FetchEntityCollection(collectionToFetch, relationInfo)
If Not collectionToFetch.Count = 1
Throw New ORMEntityValidationException("The <[RelationFieldName]> is not valid.", containingEntity)
End If
End If
<[EndIf]>
<[NextForeach]>
<[NextForeach]>
Which generates:
Dim adapter As New DatabaseSpecific.DataAccessAdapter
If entityToValidate.Fields(CType(SubprojectFieldIndex.ProjectId, Integer)).IsChanged Then
Dim relationInfo As IRelationPredicateBucket = entityToValidate.GetRelationInfoProject()
' fetch entity here
Dim collectionToFetch As New HelperClasses.EntityCollection(New FactoryClasses.MyProjectEntityFactory)
adapter.FetchEntityCollection(collectionToFetch, relationInfo)
If Not entityToFetch.Count = 1 Then
Throw New ORMEntityValidationException("The ProjectId is not valid.", containingEntity)
End If
End If
I haven't tested this yet for speed or to see how it works for new entities with new child entities. In this case how would it work if I was adding a new subproject to a project that didn't yet exist in the DB?
But that's for tomorrow...