Exemple #1
0
        private static EDM Read(XElement edmx, Action<XElement> readMoreAction)
        {
            XElement edmxRuntime = edmx.Element(XName.Get("Runtime", edmxNamespace.NamespaceName));

            SSDLContainer ssdlContainer = SSDLIO.ReadXElement(edmxRuntime);
            CSDLContainer csdlContainer = CSDLIO.ReadXElement(edmxRuntime);
            XElement edmxDesigner = edmx.Element(XName.Get("Designer", edmxNamespace.NamespaceName));
            
            if (ssdlContainer == null && csdlContainer == null)
            	return new EDM();

            EDM edm = new EDM()
            {
                SSDLContainer = ssdlContainer,
                CSDLContainer = MSLIO.IntegrateMSLInCSDLContainer(csdlContainer, ssdlContainer, edmxRuntime),
            };

            if (edmxDesigner != null)
            {
                if (edmxDesigner.Element(XName.Get("Connection", edmxNamespace.NamespaceName)) != null)
                    edm.DesignerProperties = edmxDesigner.Element(XName.Get("Connection", edmxNamespace.NamespaceName)).Element(XName.Get("DesignerInfoPropertySet", edmxNamespace.NamespaceName)).Elements(XName.Get("DesignerProperty", edmxNamespace.NamespaceName)).Select(e => new DesignerProperty { Name = e.Attribute("Name").Value, Value = e.Attribute("Value").Value });

                if (edmxDesigner.Element(XName.Get("Options", edmxNamespace.NamespaceName)) != null)
                    edm.EDMXDesignerDesignerProperties = edmxDesigner.Element(XName.Get("Options", edmxNamespace.NamespaceName)).Element(XName.Get("DesignerInfoPropertySet", edmxNamespace.NamespaceName)).Elements(XName.Get("DesignerProperty", edmxNamespace.NamespaceName)).Select(e => new DesignerProperty { Name = e.Attribute("Name").Value, Value = e.Attribute("Value").Value });

                if (edmxDesigner.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName)) != null)
                    edm.EDMXDesignerDiagrams = edmxDesigner.Element(XName.Get("Diagrams", edmxNamespace.NamespaceName)).Elements(XName.Get("Diagram", edmxNamespace.NamespaceName));
            }

            readMoreAction(edmx);
            return edm;
        }
        public static EDM CreateEDMFromIDatabase(IDatabase database, string modelNamespace)
        {
            EDM edm = new EDM();

            edm.SSDLContainer = SSDLConverter.CreateSSDLContainer(database, modelNamespace);
            
            return edm;
        }
Exemple #3
0
        public static XElement Write(EDM edm)
        {
            CSDLContainer csdlContainer = edm.CSDLContainer;
            string entityContainerNamespace = string.Concat(csdlContainer.Namespace, ".");

            // Instantiate Mapping
            XElement mapping = new XElement(mslNamespace + "Mapping",
                new XAttribute("Space", "C-S"));

            // EntityContainerMapping
            XElement entityContainerMapping = new XElement(mslNamespace + "EntityContainerMapping", 
                new XAttribute("StorageEntityContainer", edm.SSDLContainer.Name),
                new XAttribute("CdmEntityContainer", csdlContainer.Name));

            foreach (EntityType entitySet in csdlContainer.EntitySets)
            {
                IEnumerable<EntityType> entityTypes = csdlContainer.EntityTypes.Where(entityType => entityType.EntitySetName == entitySet.EntitySetName);

                // EntityContainerMapping : EntitySetMapping
                XElement entitySetMappingElement = new XElement(mslNamespace + "EntitySetMapping", 
                    new XAttribute("Name", entitySet.Name));

                // EntityContainerMapping : EntitySetMapping : EntityTypeMapping
                foreach (EntityType entityType in entityTypes)
                {
                    XElement entityTypeMappingElement = new XElement(mslNamespace + "EntityTypeMapping", 
                        new XAttribute("TypeName", string.Format("IsTypeOf({0}{1})", entityContainerNamespace, entityType.Name)));

                    // EntityContainerMapping : EntitySetMapping : EntityTypeMapping : MappingFragment
                    foreach (EDMObjects.SSDL.EntityType.EntityType table in entityType.Mapping.MappedSSDLTables)
                    { 
                        XElement mappingFragmentElement = new XElement(mslNamespace + "MappingFragment", 
                            new XAttribute("StoreEntitySet", table.EntitySetName));

                        IEnumerable<PropertyMapping> scalarMappings = entityType.Mapping.GetSpecificMappingForTable(table);

                        foreach (PropertyMapping scalarMapping in scalarMappings)
                        {
                            mappingFragmentElement.AddElement(new XElement(mslNamespace + "ScalarProperty",
                                new XAttribute("Name", scalarMapping.Property.Name),
                                new XAttribute("ColumnName", scalarMapping.Column.Name)));
                        }

                        mappingFragmentElement.Add(MappingComplexProperties(entityType, entityType.Mapping, table, entityContainerNamespace));

                        IEnumerable<ConditionMapping> conditionMappings = entityType.Mapping.ConditionsMapping.Where(condition => condition.Table == table);

                        foreach (ConditionMapping conditionMapping in conditionMappings)
                        {
                            XElement conditionElement = new XElement(mslNamespace + "Condition");

                            if (conditionMapping is ColumnConditionMapping)
                                conditionElement.AddAttribute("ColumnName", (conditionMapping as ColumnConditionMapping).Column.Name);
                            else if (conditionMapping is PropertyConditionMapping)
                                conditionElement.AddAttribute("Name", (conditionMapping as PropertyConditionMapping).CSDLProperty.Name);

                            mappingFragmentElement.Add(conditionElement.AddMappingConditionAttribute(conditionMapping));
                        }

                        entityTypeMappingElement.Add(mappingFragmentElement);
                    }

                    entitySetMappingElement.Add(entityTypeMappingElement);
                }

                // EntityContainerMapping : EntitySetMapping : CUDFunctionMapping
                foreach (EntityType entityType in entityTypes)
                {
                    entitySetMappingElement.Add(CUDFunctionMapping(entityType, entityContainerNamespace, string.Concat(edm.SSDLContainer.Namespace, ".")));
                }

                entityContainerMapping.Add(entitySetMappingElement);
            }

            // EntityContainerMapping : AssociationSetMappings
            IEnumerable<Association> associations = csdlContainer.Associations.Where(association => association.Mapping.SSDLTableMapped != null);

            foreach (Association association in associations)
            {
                XElement associationSetMappingElement = new XElement(mslNamespace + "AssociationSetMapping",
                    new XAttribute("Name", association.AssociationSetName),
                    new XAttribute("TypeName", string.Concat(entityContainerNamespace, association.Name)),
                    new XAttribute("StoreEntitySet", association.Mapping.SSDLTableMapped.Name));

                XElement endPropertyElement1 = new XElement(mslNamespace + "EndProperty",
                    new XAttribute("Name", association.PropertyEnd1Role));

                foreach (PropertyMapping navigationPropertyMapping in association.PropertyEnd1.Mapping)
                {
                    endPropertyElement1.AddElement(new XElement(mslNamespace + "ScalarProperty",
                        new XAttribute("Name", navigationPropertyMapping.Property.Name),
                        new XAttribute("ColumnName", navigationPropertyMapping.Column.Name)));
                }

                XElement endPropertyElement2 = new XElement(mslNamespace + "EndProperty",
                    new XAttribute("Name", association.PropertyEnd2Role));

                foreach (PropertyMapping navigationPropertyMapping in association.PropertyEnd2.Mapping)
                {
                    endPropertyElement2.AddElement(new XElement(mslNamespace + "ScalarProperty",
                        new XAttribute("Name", navigationPropertyMapping.Property.Name),
                        new XAttribute("ColumnName", navigationPropertyMapping.Column.Name)));
                }

                associationSetMappingElement.Add(endPropertyElement1);
                associationSetMappingElement.Add(endPropertyElement2);

                entityContainerMapping.Add(associationSetMappingElement);
            }

            // EntityContainerMapping : Conditions
            foreach (Function function in csdlContainer.Functions)
            {
                entityContainerMapping.Add(new XElement(mslNamespace + "FunctionImportMapping",
                    new XAttribute("FunctionImportName", function.Name),
                    new XAttribute("FunctionName", string.Format("{0}.{1}", edm.SSDLContainer.Namespace, function.SSDLFunction.Name))));
            }

            return mapping.AddElement(entityContainerMapping);
        }