コード例 #1
0
        /// <summary>
        /// Adds a member mapping from a conceptual model scalar member to a storage model scalar member, with a entity type specified
        /// </summary>
        /// <param name="modelMemberProperty">Conceptual model scalar member to map</param>
        /// <param name="storeMemberProperty">Storage model scalar member to map to</param>
        /// <param name="modelEntityType">Model entity type to specify in the EntityTypeMapping for this member mapping.</param>
        public void AddMemberMapping(ModelMemberProperty modelMemberProperty, StoreMemberProperty storeMemberProperty, ModelEntityType modelEntityType)
        {
            if (modelEntityType != _modelEntitySet.EntityType && !modelEntityType.IsSubtypeOf(_modelEntitySet.EntityType))
            {
                throw new ArgumentException("The model member does not belong to the mapped entity type or a subclass of the mapped entity type.");
            }

            if (storeMemberProperty.EntityType.EntitySet != null)
            {
                //find the appropriate mapping fragment
                string storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name;

                //get hold of the type mapping
                XmlElement entityTypeMapping = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);
                if (entityTypeMapping == null)
                {
                    //not found - create
                    entityTypeMapping = EDMXDocument.CreateElement("EntityTypeMapping", NameSpaceURImap);
                    _esmElement.AppendChild(entityTypeMapping);

                    entityTypeMapping.SetAttribute("TypeName", modelEntityType.FullName);
                }

                XmlElement mappingFragment = (XmlElement)entityTypeMapping.SelectSingleNode("map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);
                if (mappingFragment == null)
                {
                    mappingFragment = EDMXDocument.CreateElement("MappingFragment", NameSpaceURImap);
                    entityTypeMapping.AppendChild(mappingFragment);

                    mappingFragment.SetAttribute("StoreEntitySet", storeEntitySetName);

                    if (_storeEntitySetsEnumerated == true)
                    {
                        StoreEntitySet storeEntitySet = _csMapping.ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));
                        if (storeEntitySet != null)
                        {
                            storeEntitySet.Removed += new EventHandler(storeEntitySet_Removed);
                            _storeEntitySets.Add(storeEntitySet);
                        }
                    }
                }

                if (mappingFragment != null)
                {
                    if (mappingFragment.SelectSingleNode("map:ScalarProperty[@Name=" + XmlHelpers.XPathLiteral(modelMemberProperty.Name) + "][@ColumnName=" + XmlHelpers.XPathLiteral(storeMemberProperty.Name) + "]", NSM) == null)
                    {
                        XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                        scalarProperty.SetAttribute("Name", modelMemberProperty.Name);
                        scalarProperty.SetAttribute("ColumnName", storeMemberProperty.Name);
                        mappingFragment.AppendChild(scalarProperty);

                        _memberMappings.Add(new Tuple<StoreMemberProperty, ModelMemberProperty, ModelEntityType>(storeMemberProperty, modelMemberProperty, modelEntityType));

                        storeMemberProperty.Removed += new EventHandler(smp_Removed);
                        modelMemberProperty.Removed += new EventHandler(mmp_Removed);

                        storeMemberProperty.CSMappingsUpdated();
                        modelMemberProperty.CSMappingsUpdated();
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                }
                else
                {
                    throw new ArgumentException("The store entity set " + storeEntitySetName + " is not associated with the model entity set " + this.ModelEntitySet.Name);
                }
            }
            else
            {
                throw new InvalidOperationException("The store entity type " + (storeMemberProperty.EntityType != null ? storeMemberProperty.EntityType.Name : "[unknown]") + " is not associated with an entity set.");
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds a member mapping from a conceptual model scalar member to a storage model scalar member, with a entity type specified
        /// </summary>
        /// <param name="modelMemberProperty">Conceptual model scalar member to map</param>
        /// <param name="storeMemberProperty">Storage model scalar member to map to</param>
        /// <param name="modelEntityType">Model entity type to specify in the EntityTypeMapping for this member mapping.</param>
        public void AddMemberMapping(ModelMemberProperty modelMemberProperty, StoreMemberProperty storeMemberProperty, ModelEntityType modelEntityType)
        {
            if (modelEntityType != _modelEntitySet.EntityType && !modelEntityType.IsSubtypeOf(_modelEntitySet.EntityType))
            {
                throw new ArgumentException("The model member does not belong to the mapped entity type or a subclass of the mapped entity type.");
            }

            if (storeMemberProperty.EntityType.EntitySet != null)
            {
                //find the appropriate mapping fragment
                string storeEntitySetName = storeMemberProperty.EntityType.EntitySet.Name;

                //get hold of the type mapping
                XmlElement entityTypeMapping = (XmlElement)_esmElement.SelectSingleNode("map:EntityTypeMapping[@TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.FullName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.FullName + ")") + " or @TypeName=" + XmlHelpers.XPathLiteral(modelEntityType.AliasName) + " or @TypeName=" + XmlHelpers.XPathLiteral("IsTypeOf(" + modelEntityType.AliasName + ")") + "]", NSM);
                if (entityTypeMapping == null)
                {
                    //not found - create
                    entityTypeMapping = EDMXDocument.CreateElement("EntityTypeMapping", NameSpaceURImap);
                    _esmElement.AppendChild(entityTypeMapping);

                    entityTypeMapping.SetAttribute("TypeName", modelEntityType.FullName);
                }

                XmlElement mappingFragment = (XmlElement)entityTypeMapping.SelectSingleNode("map:MappingFragment[@StoreEntitySet=" + XmlHelpers.XPathLiteral(storeEntitySetName) + "]", NSM);
                if (mappingFragment == null)
                {
                    mappingFragment = EDMXDocument.CreateElement("MappingFragment", NameSpaceURImap);
                    entityTypeMapping.AppendChild(mappingFragment);

                    mappingFragment.SetAttribute("StoreEntitySet", storeEntitySetName);

                    if (_storeEntitySetsEnumerated == true)
                    {
                        StoreEntitySet storeEntitySet = _csMapping.ParentFile.StorageModel.EntitySets.FirstOrDefault(es => es.Name.Equals(storeEntitySetName, StringComparison.InvariantCultureIgnoreCase));
                        if (storeEntitySet != null)
                        {
                            storeEntitySet.Removed += new EventHandler(storeEntitySet_Removed);
                            _storeEntitySets.Add(storeEntitySet);
                        }
                    }
                }

                if (mappingFragment != null)
                {
                    if (mappingFragment.SelectSingleNode("map:ScalarProperty[@Name=" + XmlHelpers.XPathLiteral(modelMemberProperty.Name) + "][@ColumnName=" + XmlHelpers.XPathLiteral(storeMemberProperty.Name) + "]", NSM) == null)
                    {
                        XmlElement scalarProperty = EDMXDocument.CreateElement("ScalarProperty", NameSpaceURImap);
                        scalarProperty.SetAttribute("Name", modelMemberProperty.Name);
                        scalarProperty.SetAttribute("ColumnName", storeMemberProperty.Name);
                        mappingFragment.AppendChild(scalarProperty);

                        _memberMappings.Add(new Tuple <StoreMemberProperty, ModelMemberProperty, ModelEntityType>(storeMemberProperty, modelMemberProperty, modelEntityType));

                        storeMemberProperty.Removed += new EventHandler(smp_Removed);
                        modelMemberProperty.Removed += new EventHandler(mmp_Removed);

                        storeMemberProperty.CSMappingsUpdated();
                        modelMemberProperty.CSMappingsUpdated();
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                }
                else
                {
                    throw new ArgumentException("The store entity set " + storeEntitySetName + " is not associated with the model entity set " + this.ModelEntitySet.Name);
                }
            }
            else
            {
                throw new InvalidOperationException("The store entity type " + (storeMemberProperty.EntityType != null ? storeMemberProperty.EntityType.Name : "[unknown]") + " is not associated with an entity set.");
            }
        }