public static CSDLContainer ReadXElement(XElement edmxRuntime) { XElement schemaElement = edmxRuntime.Element(XName.Get("ConceptualModels", edmxNamespace.NamespaceName)).Element(XName.Get("Schema", csdlNamespace.NamespaceName)); if (schemaElement == null || schemaElement.IsEmpty) return null; XElement entityContainerElement = schemaElement.Element(XName.Get("EntityContainer", csdlNamespace.NamespaceName)); CSDLContainer csdlContainer = new CSDLContainer { Namespace = schemaElement.Attribute("Namespace").Value, Alias = schemaElement.Attribute("Alias").Value, Name = entityContainerElement.Attribute("Name").Value }; #region EntityTypes while (true) { var typesEnumerator = (from ete in schemaElement.Elements(XName.Get("EntityType", csdlNamespace.NamespaceName)) let eteName = ete.Attribute("Name").Value where !csdlContainer.EntityTypes.Any(et => et.Name == eteName) let baseTypeAttribute = ete.Attribute("BaseType") let baseType = baseTypeAttribute == null ? null : csdlContainer.EntityTypes.GetByName(GetName(baseTypeAttribute.Value)) where baseTypeAttribute == null || baseType != null select new { EntityTypeElement = ete, Name = eteName, BaseType = baseType }).GetEnumerator(); if (!typesEnumerator.MoveNext()) break; do { var current = typesEnumerator.Current; csdlContainer.EntityTypes.Add(ReadCSDLEntityType(schemaElement, entityContainerElement, current.EntityTypeElement, csdlContainer, current.Name, current.BaseType)); } while (typesEnumerator.MoveNext()); } #endregion EntityTypes #region Associations foreach (var association in csdlContainer.AssociationsCreated) { association.AssociationSetName = entityContainerElement.Elements(XName.Get("AssociationSet", csdlNamespace.NamespaceName)).First(ae => GetName(ae.Attribute("Association").Value) == association.Name).Attribute("Name").Value; if (association.PropertyEnd2.EntityType == null) { var entityTypeName = schemaElement.Elements(XName.Get("Association", csdlNamespace.NamespaceName)).First(ae => ae.Attribute("Name").Value == association.Name).Elements(XName.Get("End", csdlNamespace.NamespaceName)).First(er => er.Attribute("Role").Value == association.PropertyEnd2Role).Attribute("Type").Value; int dotIndex = entityTypeName.IndexOf("."); if (dotIndex != -1) entityTypeName = entityTypeName.Substring(dotIndex + 1); var entityType = csdlContainer.EntityTypes.First(et => et.Name == entityTypeName); ; entityType.NavigationProperties.Add(association.PropertyEnd2); } } #endregion Associations #region ComplexTypes foreach (var complexTypeElement in schemaElement.Elements(XName.Get("ComplexType", csdlNamespace.NamespaceName))) { var complexType = new ComplexType { Name = complexTypeElement.Attribute("Name").Value }; ReadCSDLType(schemaElement, complexTypeElement, csdlContainer, complexType); csdlContainer.ComplexTypes.Add(complexType); } #endregion ComplexTypes #region Functions foreach (var functionElement in entityContainerElement.Elements(XName.Get("FunctionImport", csdlNamespace.NamespaceName))) { var function = new Function { Name = functionElement.Attribute("Name").Value }; var returnTypeAttribute = functionElement.Attribute("ReturnType"); if (returnTypeAttribute != null) { var returnTypeValue = returnTypeAttribute.Value; returnTypeValue = returnTypeValue.Remove(returnTypeValue.IndexOf(")")).Substring(returnTypeValue.IndexOf("(") + 1); function.ScalarReturnType = GetScalarPropertyTypeFromAttribute(returnTypeValue); if (function.ScalarReturnType == null) function.EntityType = csdlContainer.EntityTypes.GetByName(GetName(returnTypeValue)); } SetVisibilityValueFromAttribute(functionElement, "methodAccess", visibility => function.Visibility = visibility); #region Function parameters foreach (var parameterElement in functionElement.Elements(XName.Get("Parameter", csdlNamespace.NamespaceName))) { var parameter = new FunctionParameter { Name = parameterElement.Attribute("Name").Value, Type = GetScalarPropertyTypeFromAttribute(parameterElement).Value }; SetEnumValueFromAttribute<ParameterMode>(parameterElement, "Mode", mode => parameter.Mode = mode); SetIntValueFromAttribute(parameterElement, "Precision", precision => parameter.Precision = precision); SetIntValueFromAttribute(parameterElement, "Scale", scale => parameter.Scale = scale); SetIntValueFromAttribute(parameterElement, "MaxLength", maxLength => parameter.MaxLength = maxLength); function.Parameters.Add(parameter); } #endregion Function parameters csdlContainer.Functions.Add(function); } #endregion Functions return csdlContainer; }
public ComplexType AddComplexType(string typeName) { var value = new ComplexType { Name = typeName }; ComplexTypes.Add(value); return value; }
public ComplexProperty(ComplexType complexType) { ComplexType = complexType; }