Esempio n. 1
0
        private void UpdateChildRelations(IEntityType et, EntityInfo ei)
        {
            foreach (var relation in et.GetReferencingForeignKeys())
            {
                if (relation.PrincipalToDependent == null)
                {
                    continue;
                }

                var relationKey = relation.PrincipalToDependent.Name;

                // child entity in child relation is the referring entity
                var declaringEntity = GetOrAddEntityInfo(relation.DeclaringEntityType);
                var declaringKeys   = relation.Properties.Select(p => p.ToEntityFieldInfo(declaringEntity));

                // parent entity in child relation is the current entity
                var principalEntity = GetOrAddEntityInfo(relation.PrincipalEntityType);
                var principalKeys   = relation.PrincipalKey.Properties.Select(p => p.ToEntityFieldInfo(principalEntity));

                var entityRelation = new EntityRelation
                {
                    FromEntity      = principalEntity,
                    ToEntity        = declaringEntity,
                    FromProperty    = relation.PrincipalToDependent.PropertyInfo,
                    ToProperty      = relation.DependentToPrincipal?.PropertyInfo,
                    FromKeys        = principalKeys.ToList(),
                    ToKeys          = declaringKeys.ToList(),
                    IsChildRelation = true
                };

                ei.AddRelation(relationKey, entityRelation);
            }
        }