Exemple #1
0
        public static DesignTimeMetadata FromXML(string xml)
        {
            DesignTimeMetadata metadata  = new DesignTimeMetadata();
            XDocument          xdoc      = XDocument.Parse(xml);
            XElement           xmetadata = xdoc.Element(NS_DATA + "Metadata");
            XElement           xdbSets   = xmetadata.Element(NS_DATA + "Metadata.DbSets");
            IEnumerable <XProcessingInstruction> ximports = xmetadata.Nodes().Where(n => n is XProcessingInstruction && (n as XProcessingInstruction).Target == "import").Cast <XProcessingInstruction>();

            foreach (XProcessingInstruction xpc in ximports)
            {
                metadata.TypeScriptImports.Add(xpc.Data);
            }

            if (xdbSets != null)
            {
                foreach (XElement xdbSet in xdbSets.Elements(NS_DATA + "DbSetInfo"))
                {
                    string xType       = xdbSet.Attribute("EntityType").Value;
                    Type   _entityType = _GetTypeFromXType(xType, xdoc);

                    DbSetInfo dbSetInfo = new DbSetInfo
                    {
                        dbSetName = (string)xdbSet.Attribute("dbSetName")
                    };
                    dbSetInfo.SetEntityType(_entityType);

                    if (xdbSet.Attributes("enablePaging").Any())
                    {
                        dbSetInfo.enablePaging = (bool)xdbSet.Attribute("enablePaging");
                    }

                    if (xdbSet.Attributes("pageSize").Any())
                    {
                        dbSetInfo.pageSize = (int)xdbSet.Attribute("pageSize");
                    }

                    if (xdbSet.Attributes("isTrackChanges").Any())
                    {
                        dbSetInfo.SetIsTrackChanges((bool)xdbSet.Attribute("isTrackChanges"));
                    }

                    metadata.DbSets.Add(dbSetInfo);

                    XElement xFields = xdbSet.Element(NS_DATA + "DbSetInfo.fieldInfos");
                    IEnumerable <XElement> fields = xFields.Elements(NS_DATA + "Field");
                    dbSetInfo.fieldInfos.AddRange(_XElementsToFieldList(fields));
                }
            }

            XElement xAssocs = xmetadata.Element(NS_DATA + "Metadata.Associations");

            if (xAssocs != null)
            {
                foreach (XElement xAssoc in xAssocs.Elements(NS_DATA + "Association"))
                {
                    Association assoc = new Association
                    {
                        name = (string)xAssoc.Attribute("name")
                    };
                    if (xAssoc.Attributes("parentDbSetName").Any())
                    {
                        assoc.parentDbSetName = (string)xAssoc.Attribute("parentDbSetName");
                    }

                    if (xAssoc.Attributes("childDbSetName").Any())
                    {
                        assoc.childDbSetName = (string)xAssoc.Attribute("childDbSetName");
                    }

                    if (xAssoc.Attributes("childToParentName").Any())
                    {
                        assoc.childToParentName = (string)xAssoc.Attribute("childToParentName");
                    }

                    if (xAssoc.Attributes("parentToChildrenName").Any())
                    {
                        assoc.parentToChildrenName = (string)xAssoc.Attribute("parentToChildrenName");
                    }

                    if (xAssoc.Attributes("onDeleteAction").Any())
                    {
                        assoc.onDeleteAction =
                            (DeleteAction)Enum.Parse(typeof(DeleteAction), xAssoc.Attribute("onDeleteAction").Value);
                    }

                    XElement xFieldRels = xAssoc.Element(NS_DATA + "Association.fieldRels");
                    if (xFieldRels != null)
                    {
                        foreach (XElement xFieldRel in xFieldRels.Elements(NS_DATA + "FieldRel"))
                        {
                            FieldRel fldRel = new FieldRel
                            {
                                parentField = (string)xFieldRel.Attribute("parentField"),
                                childField  = (string)xFieldRel.Attribute("childField")
                            };
                            assoc.fieldRels.Add(fldRel);
                        }
                    }

                    metadata.Associations.Add(assoc);
                }
            }

            return(metadata);
        }