/// <summary> /// Obtains the StoreGeneratedPattern value of an EdmProperty, given a target version and DataSpace /// </summary> /// <param name="property">The EdmProperty.</param> /// <param name="targetVersion">Used to correctly look up the StoreGeneratedPattern value in the EdmProperty</param> /// <param name="dataSpace">DataSpace where the EdmProperty lives (either CSDL or SSDL)</param> /// <returns>One of the StoreGeneratedPattern values, or String.Empty if the attribute or value does not exist</returns> public static StoreGeneratedPattern GetStoreGeneratedPatternValue( this EdmMember property, Version targetVersion, DataSpace dataSpace) { if (targetVersion == null) { throw new ArgumentNullException("targetVersion"); } if (dataSpace == DataSpace.CSSpace || dataSpace == DataSpace.OCSpace || dataSpace == DataSpace.OSpace) { throw new ArgumentException( String.Format(CultureInfo.CurrentCulture, Resources.ErrorNonValidDataSpace, dataSpace.ToString())); } if (dataSpace == DataSpace.CSpace) { // In the CSDL, StoreGeneratedPattern exists as an annotation in the EntityStoreSchemaGeneratorNamespace var sgpNamespace = SchemaManager.GetAnnotationNamespaceName(); if (String.IsNullOrEmpty(sgpNamespace)) { throw new ArgumentException( String.Format(CultureInfo.CurrentCulture, Resources.ErrorNonValidTargetVersion, targetVersion)); } MetadataProperty sgpMetadataProperty = null; if (property.MetadataProperties.TryGetValue( sgpNamespace + ":" + EdmConstants.facetNameStoreGeneratedPattern, false, out sgpMetadataProperty)) { var sgpValue = sgpMetadataProperty.Value as string; Debug.Assert( false == String.IsNullOrEmpty(sgpValue), "If we found the StoreGeneratedPattern annotation in the CSDL, why weren't we able to find a value?"); if (false == String.IsNullOrEmpty(sgpValue)) { return((StoreGeneratedPattern)Enum.Parse(typeof(StoreGeneratedPattern), sgpValue, false)); } } } else if (dataSpace == DataSpace.SSpace) { // In the SSDL, StoreGeneratedPattern exists as a facet Facet item = null; if (property.TypeUsage.Facets.TryGetValue(EdmConstants.facetNameStoreGeneratedPattern, false, out item)) { return((StoreGeneratedPattern)item.Value); } } return(StoreGeneratedPattern.None); }
public void HandleConversion_Targeting_EntityFramework_V1_Removes_UseStrongSpatialTypes_Attribute() { var inputDoc = LoadEdmx(V1EdmxWithUseStrongSpatialTypes); var handler = new UseStrongSpatialTypesHandler(EntityFrameworkVersion.Version1); var resultDoc = handler.HandleConversion(inputDoc); var nsmgr = SchemaManager.GetEdmxNamespaceManager(resultDoc.NameTable, EntityFrameworkVersion.Version1); nsmgr.AddNamespace("annotation", SchemaManager.GetAnnotationNamespaceName()); var useStrongSpatialTypeAttr = (XmlAttribute) resultDoc.SelectSingleNode( "/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/csdl:Schema/@annotation:UseStrongSpatialTypes", nsmgr); Assert.Null(useStrongSpatialTypeAttr); }
public void HandleConversion_Targeting_EntityFramework_V3_Inserts_UseStrongSpatialTypes_Attribute() { var inputDoc = LoadEdmx(V3EdmxWithoutUseStrongSpatialTypes); var handler = new UseStrongSpatialTypesHandler(EntityFrameworkVersion.Version3); var resultDoc = handler.HandleConversion(inputDoc); var nsmgr = SchemaManager.GetEdmxNamespaceManager(resultDoc.NameTable, EntityFrameworkVersion.Version3); nsmgr.AddNamespace("annotation", SchemaManager.GetAnnotationNamespaceName()); var schemaElement = (XmlElement)resultDoc.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/csdl:Schema", nsmgr); Assert.NotNull(schemaElement.Attributes["annotation", "http://www.w3.org/2000/xmlns/"]); var useStrongSpatialTypeAttr = (XmlAttribute) resultDoc.SelectSingleNode( "/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/csdl:Schema/@annotation:UseStrongSpatialTypes", nsmgr); Assert.Equal("false", useStrongSpatialTypeAttr.Value); }
/// <summary> /// If we are retargeting to a schema version which supports it, ensure /// UseStrongSpatialTypes="false" is set on the CSDL Schema Element. /// If we are retargeting to a schema version which does not support it, ensure /// UseStrongSpatialTypes is absent from the CSDL Schema Element. /// </summary> /// <param name="doc"></param> /// <returns></returns> protected override XmlDocument DoHandleConversion(XmlDocument doc) { var nsmgr = SchemaManager.GetEdmxNamespaceManager(doc.NameTable, _targetSchemaVersion); var annotationNamespace = SchemaManager.GetAnnotationNamespaceName(); var csdlSchemaElement = (XmlElement)doc.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/csdl:Schema", nsmgr); if (csdlSchemaElement != null) { var useStrongSpatialTypesAttr = csdlSchemaElement.Attributes[UseStrongSpatialTypesDefaultableValue.AttributeUseStrongSpatialTypes, annotationNamespace]; if (EdmFeatureManager.GetUseStrongSpatialTypesFeatureState(_targetSchemaVersion).IsEnabled()) { // we are retargeting to a Schema Version that supports UseStrongSpatialTypes - add UseStrongSpatialTypes="false" if it is not present if (useStrongSpatialTypesAttr == null) { useStrongSpatialTypesAttr = doc.CreateAttribute( "annotation", UseStrongSpatialTypesDefaultableValue.AttributeUseStrongSpatialTypes, annotationNamespace); useStrongSpatialTypesAttr.Value = "false"; csdlSchemaElement.Attributes.Append(useStrongSpatialTypesAttr); // setting the xmlns:annotation attribute explicitly will ensure that the XmlReader does not come up // with an auto-generated namespace prefix which may cause an NRE in the XmlEditor leading to a VS crash var annotationXmlnsAttr = doc.CreateAttribute("xmlns", "annotation", "http://www.w3.org/2000/xmlns/"); annotationXmlnsAttr.Value = annotationNamespace; csdlSchemaElement.SetAttributeNode(annotationXmlnsAttr); } } else { // we are retargeting to a Schema Version that does not support UseStrongSpatialTypes - remove UseStrongSpatialTypes if it is present if (useStrongSpatialTypesAttr != null) { csdlSchemaElement.Attributes.Remove(useStrongSpatialTypesAttr); } } } return(doc); }
internal StoreGeneratedPatternForCsdlDefaultableValue(EFElement parent) : base(parent, AttributeStoreGeneratedPattern, SchemaManager.GetAnnotationNamespaceName()) { }
public static void AddSchemaSpecificReplacements(IDictionary <string, string> replacementsDictionary, Version schemaVersion) { Debug.Assert(replacementsDictionary != null, "replacementsDictionary is null."); Debug.Assert(schemaVersion != null, "schemaVersion is null."); Debug.Assert(!replacementsDictionary.ContainsKey("$edmxversion$"), "replacementsDictionary contains key '$edmxversion$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$edmxnamespacename$"), "replacementsDictionary contains key '$edmxnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$ssdlnamespacename$"), "replacementsDictionary contains key '$ssdlnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$csdlnamespacename$"), "replacementsDictionary contains key '$csdlnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$msdlnamespacename$"), "replacementsDictionary contains key '$msdlnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$essgnamespacename$"), "replacementsDictionary contains key '$essgnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$cgnamespacename$"), "replacementsDictionary contains key '$cgnamespacename$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$annotationnamespace$"), "replacementsDictionary contains key '$annotationnamespace$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$lazyloadingattribute$"), "replacementsDictionary contains key '$lazyloadingattribute$'"); Debug.Assert( !replacementsDictionary.ContainsKey("$useStrongSpatialTypesAttribute$"), "replacementsDictionary contains key '$useStrongSpatialTypesAttribute$'"); // Set the namespace names (EDMX, CodeGen, ESSG, CSDL, MSDL, and SSDL) replacementsDictionary.Add("$edmxversion$", schemaVersion.ToString(2)); // only print Major.Minor version information replacementsDictionary.Add("$edmxnamespacename$", SchemaManager.GetEDMXNamespaceName(schemaVersion)); replacementsDictionary.Add("$ssdlnamespacename$", SchemaManager.GetSSDLNamespaceName(schemaVersion)); replacementsDictionary.Add("$csdlnamespacename$", SchemaManager.GetCSDLNamespaceName(schemaVersion)); replacementsDictionary.Add("$msdlnamespacename$", SchemaManager.GetMSLNamespaceName(schemaVersion)); replacementsDictionary.Add( "$essgnamespacename$", SchemaManager.GetEntityStoreSchemaGeneratorNamespaceName()); replacementsDictionary.Add( "$cgnamespacename$", SchemaManager.GetCodeGenerationNamespaceName()); if (EdmFeatureManager.GetLazyLoadingFeatureState(schemaVersion).IsEnabled() || EdmFeatureManager.GetUseStrongSpatialTypesFeatureState(schemaVersion).IsEnabled()) { replacementsDictionary.Add( "$annotationnamespace$", string.Format( CultureInfo.InvariantCulture, "xmlns:annotation=\"{0}\"", SchemaManager.GetAnnotationNamespaceName())); } else { replacementsDictionary.Add("$annotationnamespace$", string.Empty); } if (EdmFeatureManager.GetLazyLoadingFeatureState(schemaVersion).IsEnabled()) { replacementsDictionary.Add("$lazyloadingattribute$", "annotation:LazyLoadingEnabled=\"true\""); } else { replacementsDictionary.Add("$lazyloadingattribute$", string.Empty); } // set UseStrongSpatialTypes to false as runtime will throw exception if true (as of V3 - to be updated in later version of runtime) if (EdmFeatureManager.GetUseStrongSpatialTypesFeatureState(schemaVersion).IsEnabled()) { replacementsDictionary.Add( "$useStrongSpatialTypesAttribute$", "annotation:UseStrongSpatialTypes=\"false\""); } else { replacementsDictionary.Add("$useStrongSpatialTypesAttribute$", string.Empty); } }
/// <summary> /// Move Property's XElement before the specified position. /// If position parameter is null, the property XElement will be moved to the last position. /// </summary> internal void MoveTo(InsertPropertyPosition position) { Debug.Assert( PreviousSiblingInPropertyXElementOrder != null || NextSiblingInPropertyXElementOrder != null, "Why do we need to move the property if it is the only property?"); Debug.Assert(position != null, "InsertPropertyPosition parameter is null."); if (position != null) { // Check if the InsertPropertyPosition's InsertAt is not null. Debug.Assert(position.InsertAtProperty != null, "Why InsertPropertyPosition's InsertAt is null?"); if (position.InsertAtProperty != null) { // Instead of re-parenting the property's XElement, we are going to clone the XElement, insert the clone and delete the old XElement. // This is a workaround for an XML editor bug where re-parenting an element causes asserts. // First create the new XElement. var tempDoc = XDocument.Parse(XElement.ToString(SaveOptions.None), LoadOptions.None); var newPropertyXElement = tempDoc.Root; newPropertyXElement.Remove(); // Remove known namespaces from the element since the namespaces are already set in the parent node. // This is workaround because XDocument automatically appends the default namespace in the property XElement. foreach (var a in newPropertyXElement.Attributes()) { if (a.IsNamespaceDeclaration && (a.Value == SchemaManager.GetCSDLNamespaceName(Artifact.SchemaVersion) || a.Value == SchemaManager.GetSSDLNamespaceName(Artifact.SchemaVersion) || a.Value == SchemaManager.GetAnnotationNamespaceName())) { a.Remove(); } } var toBeDeleteElement = XElement; // format the XML we just parsed so that the XElement will have the same indenting. Utils.FormatXML(newPropertyXElement, GetIndentLevel()); // Call method that will insert the XElement to the specified location. InsertPosition = position; AddXElementToParent(newPropertyXElement); // Re-establish the links between EFElement and XElement. SetXObject(newPropertyXElement); Debug.Assert( XElement == newPropertyXElement, "Unexpected XElement value. Expected:" + newPropertyXElement + " , Actual:" + XElement); ModelItemAnnotation.SetModelItem(newPropertyXElement, this); Debug.Assert( ModelItemAnnotation.GetModelItem(newPropertyXElement) == this, "The new XElement should contain annotation to the model property."); // Delete both old XElement and the preceding whitespace. // Preceding whitespace is preferred over trailing whitespace because we don't want to remove the last property's trailing white-space since // it has different indent level than the rest (see EFElement's EnsureFirstNodeWhitespaceSeparation method). var preceedingNewLine = toBeDeleteElement.PreviousNode as XText; while (preceedingNewLine != null && String.IsNullOrWhiteSpace(preceedingNewLine.Value)) { var toBeDeletedWhiteSpace = preceedingNewLine; preceedingNewLine = preceedingNewLine.PreviousNode as XText; toBeDeletedWhiteSpace.Remove(); } toBeDeleteElement.Remove(); #if DEBUG // Assert if the property is not moved to the correct location. if (position.InsertBefore) { Debug.Assert( position.InsertAtProperty == NextSiblingInPropertyXElementOrder, "Expected next sibling property: " + position.InsertAtProperty.DisplayName + " , Actual next sibling:" + NextSiblingInPropertyXElementOrder.DisplayName); } else { Debug.Assert( position.InsertAtProperty == PreviousSiblingInPropertyXElementOrder, "Expected previous sibling property: " + position.InsertAtProperty.DisplayName + " , Actual previous sibling:" + PreviousSiblingInPropertyXElementOrder.DisplayName); } #endif } } }
internal UseStrongSpatialTypesDefaultableValue(EFElement parent) : base(parent, AttributeUseStrongSpatialTypes, SchemaManager.GetAnnotationNamespaceName()) { }
internal LazyLoadingDefaultableValue(EFElement parent) : base(parent, AttributeLazyLoadingEnabled, SchemaManager.GetAnnotationNamespaceName()) { }