private void PopulateMappingListItems()
        {
            _mappingListItems.Clear();

            // load existing mappings if we have a ref constraint and the user 
            // hasn't changed the principal role
            if (_association.ReferentialConstraint != null
                &&
                _association.ReferentialConstraint.Principal.Role.Target == _principal)
            {
                var principalType = _principal.Type.Target;
                var principalKeys = GetKeysForType(principalType);

                var pnum = _association.ReferentialConstraint.Principal.PropertyRefs.GetEnumerator();
                var dnum = _association.ReferentialConstraint.Dependent.PropertyRefs.GetEnumerator();

                // always move to the next principal ref
                while (pnum.MoveNext())
                {
                    var psym = pnum.Current.Name.NormalizedName();
                    Symbol dsym = null;
                    if (dnum.MoveNext())
                    {
                        dsym = dnum.Current.Name.NormalizedName();
                    }

                    var item = new MappingListItem(psym, dsym, IsValidPrincipalKey(psym, principalKeys));
                    _mappingListItems.Add(item.PrincipalKey, item);
                }
            }

            // add any remaining principal keys that aren't mapped yet in a 
            // ref constraint (which means all if no ref constraint exists)
            var principalEntityType = _principal.Type.Target as ConceptualEntityType;
            Debug.Assert(principalEntityType != null, "EntityType is not ConceptualEntityType");
            foreach (var key in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
            {
                if (_mappingListItems.ContainsKey(key.NormalizedName) == false)
                {
                    var item = new MappingListItem(key.NormalizedName, null, true);
                    _mappingListItems.Add(item.PrincipalKey, item);
                }
            }

            // attempt to auto-map the keys if we don't have a ref constraint
            if (_association.ReferentialConstraint == null
                &&
                principalEntityType != null
                &&
                _dependent.Type.Target != null)
            {
                // load a list of dependent properties
                var dependentProperties = new List<Symbol>();

                foreach (var dprop in GetMappableDependentProperties())
                {
                    dependentProperties.Add(dprop.NormalizedName);
                }

                // process each principal key first by name
                foreach (var pkey in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
                {
                    var item = _mappingListItems[pkey.NormalizedName];

                    // attempt a unique 1:1 name match between the principal key and any dependent property
                    if (item.DependentProperty == null)
                    {
                        item.DependentProperty = dependentProperties.Where(p => p.GetLocalName() == pkey.LocalName.Value).FirstOrDefault();
                        if (item.DependentProperty != null)
                        {
                            dependentProperties.Remove(item.DependentProperty);
                        }
                    }
                }

                // process any unmapped primary keys now ordinally
                foreach (var pkey in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
                {
                    var item = _mappingListItems[pkey.NormalizedName];
                    if (item.DependentProperty == null)
                    {
                        // are there any unmapped dependent keys?
                        if (dependentProperties.Count > 0)
                        {
                            // there are, pick the first one
                            item.DependentProperty = dependentProperties[0];
                            dependentProperties.Remove(item.DependentProperty);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private void PopulateMappingListItems()
        {
            _mappingListItems.Clear();

            // load existing mappings if we have a ref constraint and the user
            // hasn't changed the principal role
            if (_association.ReferentialConstraint != null
                &&
                _association.ReferentialConstraint.Principal.Role.Target == _principal)
            {
                var principalType = _principal.Type.Target;
                var principalKeys = GetKeysForType(principalType);

                var pnum = _association.ReferentialConstraint.Principal.PropertyRefs.GetEnumerator();
                var dnum = _association.ReferentialConstraint.Dependent.PropertyRefs.GetEnumerator();

                // always move to the next principal ref
                while (pnum.MoveNext())
                {
                    var    psym = pnum.Current.Name.NormalizedName();
                    Symbol dsym = null;
                    if (dnum.MoveNext())
                    {
                        dsym = dnum.Current.Name.NormalizedName();
                    }

                    var item = new MappingListItem(psym, dsym, IsValidPrincipalKey(psym, principalKeys));
                    _mappingListItems.Add(item.PrincipalKey, item);
                }
            }

            // add any remaining principal keys that aren't mapped yet in a
            // ref constraint (which means all if no ref constraint exists)
            var principalEntityType = _principal.Type.Target as ConceptualEntityType;

            Debug.Assert(principalEntityType != null, "EntityType is not ConceptualEntityType");
            foreach (var key in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
            {
                if (_mappingListItems.ContainsKey(key.NormalizedName) == false)
                {
                    var item = new MappingListItem(key.NormalizedName, null, true);
                    _mappingListItems.Add(item.PrincipalKey, item);
                }
            }

            // attempt to auto-map the keys if we don't have a ref constraint
            if (_association.ReferentialConstraint == null
                &&
                principalEntityType != null
                &&
                _dependent.Type.Target != null)
            {
                // load a list of dependent properties
                var dependentProperties = new List <Symbol>();

                foreach (var dprop in GetMappableDependentProperties())
                {
                    dependentProperties.Add(dprop.NormalizedName);
                }

                // process each principal key first by name
                foreach (var pkey in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
                {
                    var item = _mappingListItems[pkey.NormalizedName];

                    // attempt a unique 1:1 name match between the principal key and any dependent property
                    if (item.DependentProperty == null)
                    {
                        item.DependentProperty = dependentProperties.Where(p => p.GetLocalName() == pkey.LocalName.Value).FirstOrDefault();
                        if (item.DependentProperty != null)
                        {
                            dependentProperties.Remove(item.DependentProperty);
                        }
                    }
                }

                // process any unmapped primary keys now ordinally
                foreach (var pkey in principalEntityType.ResolvableTopMostBaseType.ResolvableKeys)
                {
                    var item = _mappingListItems[pkey.NormalizedName];
                    if (item.DependentProperty == null)
                    {
                        // are there any unmapped dependent keys?
                        if (dependentProperties.Count > 0)
                        {
                            // there are, pick the first one
                            item.DependentProperty = dependentProperties[0];
                            dependentProperties.Remove(item.DependentProperty);
                        }
                    }
                }
            }
        }